0

我正在尝试从 Azure 获取新令牌,但出现 404 错误。当我转储请求正文和参数,然后将其转换为 cURL 请求时,一切正常。有什么想法吗?

{ host: 'login.microsoftonline.com',
  method: 'POST',
  port: 443,
  path: '/common/oauth2/token',
  headers:
   { 'Content-Type': 'application/x-www-form-urlencoded'
   }  
}

请求正文:

refresh_token=SNIP&client_id=SNIP&client_secret=SNIP&grant_type=refresh_token

我知道令牌和客户信息是正确的,因为它适用于 curl..

我正在使用 Node 的https.request库进行调用。有任何想法吗?

4

1 回答 1

0

这是有关通过刷新令牌获取访问令牌的完整示例https.request

var https = require('https');
var querystring = require('querystring');

var postData = querystring.stringify({
  refresh_token: "<refresh_token>",
  client_id: "<client_id>",
  client_secret:"<client_secret>",
  grant_type:"refresh_token"
});
var options = {
  hostname: 'login.microsoftonline.com',
  port: 443,
  path: '/<tenant_id>/oauth2/token',
  method: 'POST',
  headers:
   { 
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
   } 
};



var req = https.request(options, function (res) {
  var result = '';
  res.on('data', function (chunk) {
    result += chunk;
  });
  res.on('end', function () {
    console.log(result);
  });
  res.on('error', function (err) {
    console.log(err);
  })
});

// req error
req.on('error', function (err) {
  console.log(err);
});

//send request witht the postData form
req.write(postData);
req.end();

如有任何进一步的疑虑,请随时告诉我。

于 2016-04-28T02:22:29.527 回答