我正在尝试使用我的 node.js 服务器获取令牌。
下面的代码在有人调用我的服务器的 REST-API 时执行(在此调用的处理过程中,服务器自己进行了多次调用)。
以下配置在 POSTMAN 中有效:
网址: https ://login.windows.net/MyCompanyTenant.onmicrosoft.com/oauth2/token
标题:
- 缓存控制:无缓存
- 内容类型:application/x-www-form-urlencoded
身体:
- client_id:YourClientIdFromAzureAd
- 资源:https ://myCompanyTenant.crm.dynamics.com
- 用户名:yourServiceUser@myCompanyTenant.onmicrosoft.com
- 密码:yourServiceUserPassword
- 授予类型:密码
- client_secret:YourClientSecretFromAzureAd
现在我的失败代码试图构建这个请求:
function retrieveAuthToken() {
var deferred = q.defer();
var bodyDataString = querystring.stringify({
grant_type: "password",
client_id: someClientId,
resource: someUrl,
username: someUsername,
password: someUsernamePassword,
client_secret: someString
});
//I also tried replacing thie bodyDataString by the bodyString from the working request provided by Fiddler
var options = {
host: 'login.windows.net',
path: '/someTenant/oauth2/token',
method: 'POST',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Cache-Control": "no-cache"
}
};
var request = https.request(options, function(response){
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
var parsed = JSON.parse(body); //todo: try/catch
rdeferred.resolve(parsed.access_token);
});
});
request.on('error', function(e) {
console.log(e.message);
deferred.reject();
});
request.write(bodyDataString );
return deferred.promise;
我没有看到任何错误,我的服务器只是等待并等待......