Github is a platform which hosts millions of lines of code in the form of code repositories. Github also provides Oauth based APIs which developers can integrate into their apps. In this tutorial we'll walk you through a simple demo app in which we let the user authenticate via their github account and we'll fetch the information of the authenticated user and display that information in JSON format on our screen.
Now assuming you've your github account ready , it's the time to create a developer app. Just follow these simple steps to create your github oauth app.
client_id
and client_secret
as
we'll need this information when we create our nodejs app.
Now let's create a very simple nodejs express program to authenticate users using github api and fetch details about those users. Before we go on to create our source code file , install the following modules if you don't have them installed already.
>npm install express
>npm install request
github-auth.js
and paste the following code into it.
//Name of the file : github-auth.js
// Section 1 get the requirements and initialize express app
const express = require('express');
const request = require('request');
const app = express();
// Section 2- configure variables and different urls
// config to define app settings
// use environment variables [ process.env ] for sensitive data like api keys and secrets
var config = {
client_id: process.env.github_client_id,
client_secret: process.env.github_client_secret,
redirect_url: 'http://localhost:3000/github/callback',
authorize_url:'https://github.com/login/oauth/authorize',
token_url: 'https://github.com/login/oauth/access_token',
user_url: 'https://api.github.com/user',
scope: 'user'
};
// Section-3 Define the routes and callback url
// define routes
app.get('/github/auth', function(req,res){
// redirect the user to github authorization url
return res.redirect(config.authorize_url);
});
app.get('/github/callback', function(req,res){
// extract authorize code
var code = req.query.code
// configure request params
options = {
method: 'POST',
uri: config.token_url,
formData: {
client_id : config.client_id,
client_secret : config.client_secret,
code : code
},
headers: {
accept: 'application/json'
}
};
// make a request for auth_token using above options
request(options , function(e,r,b){
// process the body
if(b) {
jb = JSON.parse(b)
// configure request to fetch user information
options_user = {
method:'GET',
url: config.user_url+'?access_token='+jb.access_token,
headers: {
accept: 'application/json',
'User-Agent': 'custom'
}
}
request(options_user , function(ee,rr,bb){
// process the body
if(bb) {
var bo = JSON.parse(bb)
var resp = {
name: bo.name ,
url: bo.url ,
id: bo.id ,
bio: bo.bio
}
return res.json(resp)
}
else {
console.log(er)
return res.json(er)
}
});
}
});
});
// Section 4 start the app
app.listen(3000, () => console.log('Njera github-api app listening on port 3000!'));
require
keyword and initialize an express app.
github_client_id
and github_client_secret
, you'll need to set them for
your app to work properly.
>export github_client_id=YOUR_CLIENT_ID
>export github_client_secret=YOUR_CLIENT_SECRET
set github_client_id=YOUR_CLIENT_ID
set github_client_secret=YOUR_CLIENT_SECRET
client id
and secret
are the same which we obtained when we created the github app. So just create these
environment variables and we're good to go.
>node github-auth.js
Once your application has launched successfully , just go to your browser at :
http://localhost:3000/github/auth
and this will take you to github for authentication and authorization , authorize your app to use your information and
once it's done your should see your basic details being displayed on the screen.
What we've discussed in this short tutorial is just a drop in the ocean, there's a lot of other informative stuff you can get using github api , to further explore the api visit github developers page.
In this chapter of 30 days of node tutorial series, we learned about nodejs and github api we accomplished the following :
access_token
.