Skip to content

Instantly share code, notes, and snippets.

@lexoyo
Last active November 21, 2015 12:25
Show Gist options
  • Save lexoyo/932550e8ddbae2ec1d2d to your computer and use it in GitHub Desktop.
Save lexoyo/932550e8ddbae2ec1d2d to your computer and use it in GitHub Desktop.
Use github API directly, without a SDK
<html>
OK LOGGED IN
<ul>
<li>
<a href='/repo'>repo<a>
</li>
</ul>
</html>
<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>
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