Skip to content

Instantly share code, notes, and snippets.

@Sneagan
Created February 5, 2014 19:44
Show Gist options
  • Save Sneagan/8831511 to your computer and use it in GitHub Desktop.
Save Sneagan/8831511 to your computer and use it in GitHub Desktop.
ADN Authorization Code POST Request
app.get('/success', function(req, res){
var postFunction = function(reqq) {
var access_token = reqq.query.code;
var post_data = querystring.stringify({
'client_id': 'client_id',
'client_secret': 'client_secret',
'grant_type': 'authorization_code',
'redirect_uri': 'http://127.0.0.1:3000/',
'code': access_token
});
var options = {
url: 'https://account.app.net/oauth/access_token?' + post_data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
var callback = function(error, response, body) {
console.log(body);
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
}
};
request.post(options, callback);
};
res.render('success', {}, postFunction(req));
})
@berg
Copy link

berg commented Feb 5, 2014

Ah, got it. You're sending the parameters as part of the URL, instead of the request body. It looks like you're using the request library, so the solution is quite simple:

app.get('/success', function(req, res){
  var postFunction = function(reqq) {
    var access_token = reqq.query.code;
    // request will encode this for you
    var post_data = {
      'client_id': 'client_id',
      'client_secret': 'client_secret',
      'grant_type': 'authorization_code',
      'redirect_uri': 'http://127.0.0.1:3000/',
      'code': access_token
    };

    var options = {
      // instead of concatting this to the URL, request
      // that it be encoded and sent as form data.
      // request will add the appropriate headers & encode for you
      url: 'https://account.app.net/oauth/access_token',
      form: post_data
    };
   var callback = function(error, response, body) {
      console.log(body);
      if (!error && response.statusCode == 200) {
        var info = JSON.parse(body);
      }
    };
    request.post(options, callback);
  };

  res.render('success', {}, postFunction(req));
})

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