节点JS – 构造一个OAuth2请求

我试图构build一个OAuth2请求的框API。 他们给出的示例POST请求作为指导对我来说有点模糊,因为我最近学习了后端开发。 示例如下:

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> 

官方文档: https : //box-content.readme.io/docs/app-auth

我试图做到这一点的方式如下:

 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); }); 

我得到以下错误:

 { "error":"invalid_request", "error_description":"Invalid grant_type parameter or parameter missing" } 

很显然授予types是不正确的,但我不知道如何去基于Box API示例构buildstring。 如果任何人都可以帮助,甚至暴露我一些好的文章或教程如何做到这一点,这将是伟大的!

谢谢。

我只是自己努力。 我能够通过将当前在boxOptions.form中的所有东西移动到请求正文中来实现这一点。

例如:

 var boxHeaders = { 'Content-Type': 'application/x-www-form-urlencoded' }; var boxOptions = { url: 'https://api.box.com/oauth2/token', method: 'POST', headers: boxHeaders }; var form = { grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer', client_id: 'id', client_secret: 'secret', assertion: boxtoken }; var request = https.request(boxOptions, function(response) { // do stuff }); request.write(querystring.stringify(form)); request.end(); 

希望这可以帮助。 不幸的是,我不太熟悉请求库来提供一个使用它的例子。