| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/gavin/workspace/happymandarin/node_modules/oauth/examples/ |
Upload File : |
var http = require('http');
var qs = require('querystring');
// var OAuth = require('oauth'), OAuth2 = OAuth.OAuth2;
var OAuth2 = require('../lib/oauth2.js').OAuth2;
var clientID = '';
var clientSecret = '';
var oauth2 = new OAuth2(clientID,
clientSecret,
'https://github.com/',
'login/oauth/authorize',
'login/oauth/access_token',
null); /** Custom headers */
http.createServer(function (req, res) {
var p = req.url.split('/');
pLen = p.length;
/**
* Authorised url as per github docs:
* https://developer.github.com/v3/oauth/#redirect-users-to-request-github-access
*
* getAuthorizedUrl: https://github.com/ciaranj/node-oauth/blob/master/lib/oauth2.js#L148
* Adding params to authorize url with fields as mentioned in github docs
*
*/
var authURL = oauth2.getAuthorizeUrl({
redirect_uri: 'http://localhost:8080/code',
scope: ['repo', 'user'],
state: 'some random string to protect against cross-site request forgery attacks'
});
/**
* Creating an anchor with authURL as href and sending as response
*/
var body = '<a href="' + authURL + '"> Get Code </a>';
if (pLen === 2 && p[1] === '') {
res.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/html' });
res.end(body);
} else if (pLen === 2 && p[1].indexOf('code') === 0) {
/** Github sends auth code so that access_token can be obtained */
var qsObj = {};
/** To obtain and parse code='...' from code?code='...' */
qsObj = qs.parse(p[1].split('?')[1]);
/** Obtaining access_token */
oauth2.getOAuthAccessToken(
qsObj.code,
{'redirect_uri': 'http://localhost:8080/code/'},
function (e, access_token, refresh_token, results){
if (e) {
console.log(e);
res.end(e);
} else if (results.error) {
console.log(results);
res.end(JSON.stringify(results));
}
else {
console.log('Obtained access_token: ', access_token);
res.end( access_token);
}
});
} else {
// Unhandled url
}
}).listen(8080);