1
votes

I'm pretty new to Express.js and the Github OAuth api and running into a wall.

The flow I've got going is, the user clicks on a link from the Ember.js application which points to a route on the Express server. Which redirects to the Github oauth route.

router.route('/oauth')
  .get(function(req, res){
    res.redirect('https://github.com/login/oauth/authorize?client_id=XXXX&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&state=1234')
  })

After the app is authenticated, the user gets redirected back to a callback route

router.route('/callback')
  .get(function(req, res){
    var code = req.query.code
    res.redirect('https://github.com/login/oauth/access_token?client_id=XXXX&client_secret=YYYY&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fgood&code=' + code)
  })

Which comes back with a code. From this point, I use that code to get an access token. The access token comes back but it comes back as a file that downloads to my machine instead of a response to my server. What am I missing?

1

1 Answers

1
votes

Don't do the second redirect. Instead you want to do a GET request for the token exchange. You're redirect url param must match the original redirect url(make sure it is url encoded): http%3A%2F%2Flocalhost%3A8080%2Fcallback

var https = require('https');

var options = {
  hostname: 'github.com',
  port: 443,
  path: '/login/oauth/access_token?client_id=XXXX&client_secret=YYYY&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback&code=' + code',
  method: 'GET'
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);

  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});

https://nodejs.org/api/https.html