簡體   English   中英

從 Microsoft Graph for Azure 獲取訪問令牌

[英]Get access token from Microsoft Graph for Azure

我在下面有這個代碼來嘗試從 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);
});

我找到了答案。 實際上帖子的數據不應該作為body發送,而是作為form發送。

因此,通過在下面執行此操作,您將獲得訪問令牌:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM