如何使用 NodeJs 调用 REST API 使用客户端 ID、租户 ID、azure AD 的客户端机密生成授权承载令牌?

How to generate Authorization Bearer token using client ID , tenant Id, Client secret of azure AD using NodeJs for calling REST API?

我可以在 Postman 中生成令牌:使用以下详细信息。

tenant_id: 09872XXXXXXXXXXXXXXXXXX
grant_type: client_credentials
client_id: d7b7e-ighewiojwoei9-868767
client_secret:adat-XXXXXX-diupi825tfsq38XXXXX
resource: https://management.azure.com/

想使用 NodeJs 实现同样的效果吗?

你用的是Azure AD client credential flow v1.0, to do this in node.js, you could use the ADAL for Node.js,把resource改成https://management.azure.com/applicationId就是你用的client_id

var AuthenticationContext = require('adal-node').AuthenticationContext;

var authorityHostUrl = 'https://login.windows.net';
var tenant = 'myTenant.onmicrosoft.com'; // AAD Tenant name.
var authorityUrl = authorityHostUrl + '/' + tenant;
var applicationId = 'yourApplicationIdHere'; // Application Id of app registered under AAD.
var clientSecret = 'yourAADIssuedClientSecretHere'; // Secret generated for app. Read this environment variable.
var resource = 'https://management.azure.com/'; // URI that identifies the resource for which the token is valid.

var context = new AuthenticationContext(authorityUrl);

context.acquireTokenWithClientCredentials(resource, applicationId, clientSecret, function(err, tokenResponse) {
  if (err) {
    console.log('well that didn\'t work: ' + err.stack);
  } else {
    console.log(tokenResponse);
  }
});