簡體   English   中英

Microsoft Graph API 身份驗證錯誤:“訪問令牌驗證失敗。 無效的觀眾”

[英]Microsoft Graph API auth error: “Access token validation failure. Invalid audience”

好的,所以我在過去的兩天里一直在處理這個錯誤,並且剛剛找到了解決方案。 在我的搜索中,我沒有找到解決我遇到的問題的單一答案(而是找到了多個最終將我指向解決方案的答案)。 因此,這是我嘗試向您解釋“訪問令牌驗證失敗。無效的受眾”錯誤的解決方案:

TLDR:

/**
 * Retrieve token for backend
 */
export const getToken = async (account): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: [process.env.REACT_APP_API_SCOPE as string],
    redirectUri: current_url,
    account,
  });
};

/**
 * Retrieve token for Microsoft Graph API:
 */
export const getTokenForGraphApi = async (
  account
): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: ["https://graph.microsoft.com/User.Read"],
    redirectUri: current_url,
    account,
  });
};

這是我如何發現的長篇故事:

我希望能夠從 React 應用程序中查詢Microsoft Graph API

我已經讓我的組織的管理員設置了 Azure 門戶,以便我們的應用注冊具有 API 權限:

  • 后端 API 權限
  • 微軟圖形
    • “用戶.閱讀”
    • “用戶.ReadBasic.All”。

在 React 中,當我進行身份驗證時,我使用了范圍:

scopes: [
    process.env.REACT_APP_API_SCOPE as string,
    "User.Read",
],

身份驗證順利,我得到了一個訪問令牌。

訪問令牌適用於我們的后端 API,但是當我嘗試將訪問令牌與 Microsoft Graph API 一起使用時,我收到錯誤消息:

“訪問令牌驗證失敗。無效的觀眾”。

我閱讀並搜索了論壇,並嘗試使用 jwt.ms。

只有我們的 API 被列為“aud”,因此我懷疑我需要一個令牌,其中放置了我們的 API 和“https://graph.microsoft.com”。

然后我嘗試在我的 User.Read scope 之前使用“https://graph.microsoft.com”,所以它會是:

scopes: [
    process.env.REACT_APP_API_SCOPE as string,
    "https://graph.microsoft.com/User.Read"
],

但它未能通過錯誤消息進行身份驗證:

"AADSTS28000: Provided value for the input parameter scope is not valid because it contains more than one resource. Scope api://{API-application-id}/a-scope https://graph.microsoft.com/User.Read openid 配置文件無效。”

在這里,我們的后端是一個資源,它有一個范圍,“https://graph.microsoft.com”是另一個資源,scope“User.Read”。

因此,解決方案是需要兩個單獨的訪問令牌:一個帶有 scope“https://graph.microsoft.com/User.Read”,您可以與圖形 api 一起使用,另一個訪問令牌用於您的后端:

/**
 * Retrieve token for backend
 */
export const getToken = async (account): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: [process.env.REACT_APP_API_SCOPE as string],
    redirectUri: current_url,
    account,
  });
};

/**
 * Retrieve token for Microsoft Graph API:
 */
export const getTokenForGraphApi = async (
  account
): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: ["https://graph.microsoft.com/User.Read"],
    redirectUri: current_url,
    account,
  });
};

錯誤消息:“訪問令牌驗證失敗。無效的受眾”告訴您“AUD”(受眾)在您與 Microsoft Graph API 一起使用的訪問令牌上設置不正確。

您可以通過粘貼訪問令牌來使用https://jwt.ms/檢查您的令牌。 (微軟在網站 jwt.ms 網站后面: https://docs.microsoft.com/en-us/azure/active-directory/develop/access-tokens

因此,解決方案是需要兩個單獨的訪問令牌:一個帶有 scope“https://graph.microsoft.com/User.Read”,您可以與 Microsoft graph api 一起使用,另一個訪問令牌用於您的后端:

/**
 * Retrieve token for backend
 */
export const getToken = async (account): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: [process.env.REACT_APP_API_SCOPE as string],
    redirectUri: current_url,
    account,
  });
};

/**
 * Retrieve token for Microsoft Graph API:
 */
export const getTokenForGraphApi = async (
  account
): Promise<AuthenticationResult> => {
  return await msalInstance.acquireTokenSilent({
    scopes: ["https://graph.microsoft.com/User.Read"],
    redirectUri: current_url,
    account,
  });
};

然后將在兩個訪問令牌上正確設置 AUD(受眾)。

暫無
暫無

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

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