Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active October 9, 2022 09:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cowboy/491b17ffb99d6838bdd2 to your computer and use it in GitHub Desktop.
Save cowboy/491b17ffb99d6838bdd2 to your computer and use it in GitHub Desktop.
node.js: in-browser github oauth from cli
var githubOAuth = require('github-oauth')({
githubClient: process.env['GITHUB_CLIENT'],
githubSecret: process.env['GITHUB_SECRET'],
baseURL: 'http://localhost:9001',
loginURI: '/login',
callbackURI: '/callback',
scope: 'user',
});
require('http').createServer(function(req, res) {
if (req.url.match(/login/)) return githubOAuth.login(req, res);
if (req.url.match(/callback/)) {
githubOAuth.callback(req, res);
res.end('You may close this window now.');
}
}).listen(9001);
githubOAuth.on('error', function(err) {
console.error('there was a login error', err);
process.exit();
});
githubOAuth.on('token', function(token, serverResponse) {
console.log('<TOKEN=%s>', token.access_token);
// do next steps instead of quitting
process.exit();
});
require('openurl').open('http://localhost:9001/login');
{
"name": "node-github-oauth-wtf",
"version": "1.0.0",
"description": "such a thing is possible :p",
"main": "index.js",
"scripts": {
"install": "node ."
},
"dependencies": {
"github-oauth": "^0.2.1",
"openurl": "^1.1.0"
}
}
@cowboy
Copy link
Author

cowboy commented Apr 23, 2015

  1. You have to register a GitHub application first:
    • Homepage URL = http://localhost:9001
    • Authorization callback URL = http://localhost:9001/callback
  2. Then save these two files in a new directory.
  3. Then take the "Client ID" and "Client Secret" values and fill in the blanks in your shell:
GITHUB_CLIENT=_____ GITHUB_SECRET=_____ npm install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment