简体   繁体   English

如何使用 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?

I am able to generate the token in Postman: using the following details.我能够在 Postman 中生成令牌:使用以下详细信息。

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

Want to achieve the same using NodeJs?想要使用 NodeJs 实现相同的目标吗?

What you are using is the Azure AD client credential flow v1.0 , to do this in node.js, you could use the ADAL for Node.js , change the resource to https://management.azure.com/ , the applicationId is the client_id you used. What you are using is the Azure AD client credential flow v1.0 , to do this in node.js, you could use the ADAL for Node.js , change the resource to https://management.azure.com/ , the applicationId is the client_id you用过的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用客户端 ID 和密码对 azure 密钥库进行身份验证? - how to authenticate azure key vault using client id and secret? 使用 Azure AD 承载令牌返回容器列表时身份验证失败 [Azure Blob] [Azure AD OAuth 2.0] [REST API] - Authentication Failed while using Azure AD Bearer Token, to return list of containers [Azure Blob] [Azure AD OAuth 2.0] [REST API] google-api-nodejs-client /如何解码从oauth2Client.getToken收到的id_token - google-api-nodejs-client / How to decode the id_token received from oauth2Client.getToken 如何从 header 授权中的不记名令牌中获取用户 ID - how to get the user id from bearer token in header authorization 如何获取NodeJS客户端ID? - How to get NodeJS client id? 对 Postman nodejs 使用承载授权 - Using bearer authorization with Postman nodejs 如何检查 Google Client_ID 和 Client_Secret 是否有效 - How to check if Google Client_ID and Client_Secret Valid or not 如何使用 nodejs 和 node-fetch 从 Azure 为 Microsoft Graph 获取客户端令牌 - How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch 如何在不使用google-api-nodejs-client的情况下处理Google API access_token过期? - How to handle Google API access_token expired without using google-api-nodejs-client? 使用 NodeJS 在 mpesa api 中获取授权令牌 - Getting the authorization token in mpesa api using NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM