【问题标题】:Microsoft Graph API: Access token validation failure. Invalid audienceMicrosoft Graph API:访问令牌验证失败。无效的观众
【发布时间】:2021-06-13 22:58:54
【问题描述】:

我正在尝试将我的应用从 Office 365 REST v2.0 迁移到 Microsoft Graph (v1.0)。令牌交换似乎正在工作,但一旦我尝试调用 API,就会收到以下错误:

    (
    [errorNumber] => 401
    [error] => Request returned HTTP error 401
    [message] => {
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure. Invalid audience.",
    "innerError": {
      "date": "2021-03-16T15:36:21",
      "request-id": "dda1e33a-2774-4986-8c45-1487404fbb72",
      "client-request-id": "e842d9a8-d71b-0563-f1ce-e58052e5bdb9"
    }
  }
}
)

access_token 有以下受众:

"aud": "https://outlook.office.com"

这是我正在使用的端点:

https://login.microsoftonline.com/common/oauth2/v2.0/token

有效载荷:

grant_type=authorization_code
&code=0.AR8A3XwQy0FAmkSxxxx
&redirect_uri=https%3A%2F%2Fxxx.com%2Fproxy%2Foffice365authorize
&client_id=e2147faf-87f0-4e7f-xxxx-xxxxxxxxxxx
&client_secret=xxxxxxxxxxxx

任何提示将不胜感激,谢谢!

【问题讨论】:

    标签: oauth-2.0 azure-active-directory jwt microsoft-graph-api


    【解决方案1】:

    这意味着您的令牌有错误的受众,要调用 Micrsoft Graph API,您需要获取 Microsoft Graph 的令牌,即访问令牌需要"aud": "https://graph.microsoft.com"

    看起来您正在使用AAD auth code flow 获取令牌,因此当您请求授权码时,请使用https://graph.microsoft.com/.default 的范围。

    https://login.microsoftonline.com/common/oauth2/authorize?
    client_id=xxxxx
    &response_type=code
    &redirect_uri=xxxxxx
    &response_mode=query
    &scope=https://graph.microsoft.com/.default
    &state=12345
    

    在请求令牌时也使用scope=https://graph.microsoft.com/.default

    POST https://login.microsoftonline.com/common/oauth2/v2.0/token
    
    client_id=xxxxxx
    &scope=https://graph.microsoft.com/.default
    &code=0.AR8A3XwQy0FAmkSxxxx
    &redirect_uri=xxxxxx
    &grant_type=authorization_code
    &client_secret=xxxxx
    

    要成功调用 API,还要确保您的客户端应用程序有 grant correct Delegated Microsoft Graph API permissions,这取决于您要调用的 API,例如如果你想调用List users,你需要here的权限。

    【讨论】:

    • 感谢您的回答。如果我添加您的建议,那么 API 会抛出此异常:Message: AADSTS70011: The provided request must include a 'scope' input parameter. The provided value for the input parameter 'scope' is not valid. The scope openid offline_access Calendars.ReadWrite https://graph.microsoft.com/.default is not valid. .default scope can't be combined with resource-specific scopes.
    • @nimrod 从scope 中删除openid offline_access Calendars.ReadWrite,只需https://graph.microsoft.com/.default 就足够了,如我的答案所示,它将获得默认情况下添加到AD 应用程序的所有MS Graph 权限的令牌。
    • 我刚刚发现该应用使用了另一个我配置的登录网址,这导致了问题:scope=openid+offline_access+outlook.office.com/Calendars.ReadWrite感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2017-11-26
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多