Last active
November 21, 2015 12:25
-
-
Save lexoyo/932550e8ddbae2ec1d2d to your computer and use it in GitHub Desktop.
Use github API directly, without a SDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
OK LOGGED IN | |
<ul> | |
<li> | |
<a href='/repo'>repo<a> | |
</li> | |
</ul> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<p> | |
Well, hello there! | |
</p> | |
<p> | |
We're going to now talk to the GitHub API. Ready? | |
<a href="https://github.com/login/oauth/authorize?scope=public_repo&client_id=c8800f58f43d89832c96">Click here</a> to begin!</a> | |
</p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var connect = require('connect') | |
var serveStatic = require('serve-static') | |
var url = require('url') | |
var fs = require('fs') | |
var app = connect() | |
var client_id = 'c8800f58f43d89832c96' | |
var client_secret = 'e218b53b2097a4415f136247b140d31aed18b176' | |
//app.use(serveStatic(__dirname)) | |
app.use('/callback', callbackRoute) | |
app.use('/login', loginRoute) | |
app.use('/admin', adminRoute) | |
app.use('/repo', repoRoute) | |
app.listen(8080) | |
console.log('http://0.0.0.0:8080/') | |
var session = {} | |
function loginRoute (req, res) { | |
res.writeHead(200) | |
res.write(fs.readFileSync(__dirname + '/login-template.html')) | |
res.end() | |
} | |
function callbackRoute (req, res) { | |
var url_parts = url.parse(req.url, true) | |
var query = url_parts.query | |
console.log('callbackRoute', query.code) | |
load({ | |
client_id: client_id, | |
client_secret: client_secret, | |
code: query.code, | |
}, { | |
'Accept': 'application/json', | |
}, https, 'github.com', null, '/login/oauth/access_token', 'post', | |
function(dataStr) { | |
var data = JSON.parse(dataStr) | |
session.access_token = data.access_token | |
session.token_type = data.token_type | |
session.scope = data.scope | |
console.log('Auth complete: ', session) | |
res.writeHead(301, {Location: '/admin'}) | |
res.end() | |
}) | |
} | |
function adminRoute (req, res) { | |
if(!session.access_token) { | |
console.log('adminRoute REDO AUTH') | |
res.writeHead(301, {Location: 'https://github.com/login/oauth/authorize?scope=public_repo&client_id=' + client_id}) | |
res.end() | |
return; | |
} | |
res.writeHead(200) | |
res.write(fs.readFileSync(__dirname + '/admin-template.html')) | |
res.end() | |
} | |
function repoRoute (req, res) { | |
if(!session.access_token) { | |
console.log('repoRoute REDO AUTH') | |
res.writeHead(301, {Location: 'https://github.com/login/oauth/authorize?scope=public_repo&client_id=' + client_id}) | |
res.end() | |
return; | |
} | |
console.log('reposRoute', session) | |
load({ | |
access_token: session.access_token, | |
affiliation: 'owner', | |
}, { | |
'Accept': 'application/json', | |
'User-Agent': 'Unifile', | |
}, https, 'api.github.com', null, '/user/repos', 'get', | |
function(dataStr) { | |
var data = JSON.parse(dataStr) | |
console.log('Data: ', data) | |
res.writeHead(200) | |
res.write(dataStr) | |
res.end() | |
}) | |
} | |
// We need this to build our post string | |
var querystring = require('querystring') | |
var https = require('https') | |
var http = require('http') | |
var fs = require('fs') | |
function load(data, headers, protocol, host, port, path, method, cbk) { | |
var post_data = querystring.stringify(data) | |
// An object of options to indicate where to post to | |
headers['Content-Length'] = Buffer.byteLength(post_data) | |
var post_options = { | |
host: host, | |
port: port, | |
path: path + '?' + post_data, | |
method: method, | |
headers: headers | |
} | |
// Set up the request | |
var post_req = protocol.request(post_options, function(res) { | |
res.setEncoding('utf8') | |
var data = '' | |
res.on('data', function (chunk) { | |
console.log('Response: ' + chunk) | |
data += chunk | |
}) | |
res.on('end', function () { | |
console.log('End: ' + data) | |
cbk(data) | |
}) | |
}) | |
// post the data | |
post_req.write(post_data) | |
post_req.end() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment