从Node.js脚本获取Microsoft GRAPH访问令牌 [英] Get Microsoft GRAPH access token from Nodejs script

查看:36
本文介绍了从Node.js脚本获取Microsoft GRAPH访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于

这是获取访问令牌的代码.

  auth.getAccessToken = function(){var deferred = Q.defer();//这些是OAuth 2.0客户端凭据授予流程所需的参数.//有关更多信息,请参阅使用客户端凭据进行服务到服务的调用(https://msdn.microsoft.com/library/azure/dn645543.aspx).var requestParams = {grant_type:"client_credentials",client_id:config.clientId,client_secret:config.clientSecret,资源:"https://graph.microsoft.com"};//向令牌发行端点发出请求.request.post({url:config.tokenEndpoint,form:requestParams},function(err,response,body){var parsedBody = JSON.parse(body);console.log(parsedBody);如果(错误){deferred.reject(err);} else if(parsedBody.error){deferred.reject(parsedBody.error_description);} 别的 {//如果成功,则返回访问令牌.deferred.resolve(parsedBody.access_token);}});返回deferred.promise;}; 

您将成功获取访问令牌.

This question builds on How to get Microsoft Graph API Access token from Node Script?, however, as a first-time user of, I don't have the required reputation for commenting on the accepted answer in that thread.

The thing is, I tried to implement the approach suggested by the accepted answer, but somewhere it goes wrong. The below code is part of an async function, and I can already tell you that the ONEDRIVE_TENANT_URI is of the format XXX.onmicrosoft.com.

const endpoint = `https://login.microsoftonline.com/${process.env.ONEDRIVE_TENTANT_URI}/oauth2/token`;
const requestParams = {
  grant_type: "client_credentials",
  client_id: process.env.ONEDRIVE_APP_ID,
  client_secret: process.env.ONEDRIVE_CLIENT_SECRET,
  resource: "https://graph.windows.net"
};

const authResponse = await request.post({
  url: endpoint,
  form: requestParams
});

authResponse gets, as its body, just a string with the requestParams as defined above filled out.

If I submit the post request via Postman, with the same parameters as x-www-form-urlencoded, I DO get an access_token in the response body.

So... What do I do wrong? Maybe - but I don't think so - it's because this function is invoked by a (for testing purposes) POSTMAN GET request with a json-formatted body?

解决方案

You can download the sample here. And fill in the credentials in config.js. You can find them from Azure portal.

This is the code to get access token.

auth.getAccessToken = function () {
  var deferred = Q.defer();

  // These are the parameters necessary for the OAuth 2.0 Client Credentials Grant Flow.
  // For more information, see Service to Service Calls Using Client Credentials (https://msdn.microsoft.com/library/azure/dn645543.aspx).
  var requestParams = {
    grant_type: 'client_credentials',
    client_id: config.clientId,
    client_secret: config.clientSecret,
    resource: 'https://graph.microsoft.com'
  };

  // Make a request to the token issuing endpoint.
  request.post({ url: config.tokenEndpoint, form: requestParams }, function (err, response, body) {
    var parsedBody = JSON.parse(body);
    console.log(parsedBody);
    if (err) {
      deferred.reject(err);
    } else if (parsedBody.error) {
      deferred.reject(parsedBody.error_description);
    } else {
      // If successful, return the access token.
      deferred.resolve(parsedBody.access_token);
    }
  });

  return deferred.promise;
};

You will get the access token successfully.

这篇关于从Node.js脚本获取Microsoft GRAPH访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆