简体   繁体   English

从 Microsoft Graph for Azure 获取访问令牌

[英]Get access token from Microsoft Graph for Azure

I have this code below to try to get an access token from Microsoft Graph.我在下面有这个代码来尝试从 Microsoft Graph 获取访问令牌。 Using the query in Postman works great.在 Postman 中使用查询效果很好。 But whenever I tried in node I am having this error: The request body must contain the following parameter: 'grant_type'.但是每当我在节点中尝试时,我都会遇到此错误:请求正文必须包含以下参数:'grant_type'。

What am I missing here please?请问我在这里缺少什么? Im really stuck.我真的卡住了。 (the value for grant_type is 'password') (grant_type 的值为“密码”)

// GET TOKEN
alterState(state => {
  const { host, userName, password, scope, client_secret, client_id, tenant_id, grant_type } = state.configuration;

  const data = {
    grant_type: grant_type,
    client_id: client_id,
    client_secret: client_secret,
    scope: scope,
    userName: userName,
    password: password,
  };

  return post(
    `${host}${tenant_id}/oauth2/v2.0/token`,
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: data
      
    },
    state => {
      console.log(state);
    }
  )(state);
});

I found the answer.我找到了答案。 Actually data for the post should not be sent as body but as form .实际上帖子的数据不应该作为body发送,而是作为form发送。

So by doing this below you have the access token:因此,通过在下面执行此操作,您将获得访问令牌:

return post(
    url,
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      form: data
      
    })

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

相关问题 如何从Microsoft Graph API获取访问令牌? - How do I get an access token from Microsoft Graph API? 如何从节点脚本获取 Microsoft Graph API 访问令牌? - How to get Microsoft Graph API Access token from Node Script? 从 Nodejs 脚本获取 Microsoft GRAPH 访问令牌 - Get Microsoft GRAPH access token from Nodejs script microsoft-graph api:从图中的刷新令牌获取新的访问令牌,而无需重定向URL - microsoft-graph api : Get new access token from refresh token in graph without redirect url 从Azure获取令牌以访问Microsoft Graph后无法调用函数 - Unable to call function after getting token back from Azure to access Microsoft Graph 如何使用 nodejs 和 node-fetch 从 Azure 为 Microsoft Graph 获取客户端令牌 - How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch Microsoft Graph API-无法刷新访问令牌 - Microsoft Graph API - cannot refresh access token 微软图形无法获得刷新令牌 - microsoft graph cant get refresh token 如何使用 Microsoft Graph Javascript 客户端库获取刷新令牌? - How to get a refresh token using the Microsoft Graph Javascript client library? 从passport-azure-ad.OIDCStrategy获取access_token用作承载令牌 - Get access_token from passport-azure-ad.OIDCStrategy for use as bearer token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM