从Microsoft Graph for Azure获取访问令牌

时间:2020-10-31 20:02:44

标签: node.js reactjs azure-active-directory microsoft-graph-api

我下面有这段代码,试图从Microsoft Graph获取访问令牌。在Postman中使用查询效果很好。但是,每当我在节点上尝试时,都会出现此错误: 请求正文必须包含以下参数:'grant_type'。

我在这里想念什么?我真的很困。 (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);
});

1 个答案:

答案 0 :(得分:0)

我找到了答案。实际上,帖子的数据不应以正文的形式发送,而应以表格的形式发送。

因此,通过下面的操作,您将获得访问令牌:

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