Node JS - Constructing an OAuth2 Request

45 views
Skip to first unread message

aaron balthaser

unread,
May 13, 2016, 9:53:13 AM5/13/16
to nodejs
Im trying to construct an OAuth2 request to the Box API. The example POST request they give as a guideline is a bit ambiguous to me as I am recently learning backend development. The example is as follows:

POST /token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&
assertion=<JWT>&
client_id=<client_id>&
client_secret=<client_secret>
Official Docs: https://box-content.readme.io/docs/app-auth
The way I attempted to do this is as follows:
var boxHeaders = {
  'Content-Type': 'application/x-www-form-urlencoded'
};

var boxOptions = {
  url: 'https://api.box.com/oauth2/token',
  method: 'POST',
  headers: boxHeaders,
  form: {
    'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion': boxtoken,
    'client_id': 'myclientid',
    'client_secret': 'myclientsecret'
  }
};

request.post(boxOptions, function(err, response, body) {
  console.log(body);
});
I get the following error:
{
  "error":"invalid_request",
  "error_description":"Invalid grant_type parameter or parameter missing"
}

Obviously the grant type is incorrect but I have no idea how to go about constructing the string based on the Box API example. If anyone can help and even expose me to some good articles or tutorials on how to do this, that would be great!

Thank you.

Reply all
Reply to author
Forward
0 new messages