簡體   English   中英

Node JS - 構建 OAuth2 請求

[英]Node JS - Constructing an OAuth2 Request

我正在嘗試向 Box API 構建 OAuth2 請求。 他們作為指導的 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"
}

顯然授權類型不正確,但我不知道如何根據 Box API 示例構建字符串。 如果有人可以幫助我,甚至讓我看到一些關於如何做到這一點的好文章或教程,那就太好了!

謝謝你。

我自己也在為此苦苦掙扎。 我能夠通過將您當前在 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();

希望這可以幫助。 不幸的是,我對請求庫不夠熟悉,無法提供使用它的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM