【问题标题】:Call Microsoft Graph API Using Azure AD 1.0 Endpoint Client Credentials Flow使用 Azure AD 1.0 终结点客户端凭据流调用 Microsoft Graph API
【发布时间】:2017-09-08 02:44:04
【问题描述】:

是否可以使用通过 Azure Active Directory 1.0 端点获得的访问令牌和客户端凭据 OAuth 2 流来访问 Microsoft Graph API?

例如:

POST https://login.microsoftonline.com/{mytenant}.onmicrosoft.com/oauth2/token
grant_type=client_credentials,
client_id={app id registered in azure portal},
client_secret={registered app key},
resource=https://graph.microsoft.com

当我使用从该请求返回的令牌时,尝试调用 https://graph.microsoft.com/v1.0/groups 时出现以下错误。

解码的 JWT

标题

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "HHByKU-0DqAqMZh6ZFPd2VWaOtg",
  "kid": "HHByKU-0DqAqMZh6ZFPd2VWaOtg"
}

有效载荷

{
  "aud": "00000002-0000-0000-c000-000000000000",
  "iss": "https://sts.windows.net/{tenant id}/",
  "iat": 1504804880,
  "nbf": 1504804880,
  "exp": 1504808780,
  "aio": "Y2FgYDiiO8/s3smXRdxLg87zBPRNAwA=",
  "appid": "{client id}",
  "appidacr": "1",
  "idp": "https://sts.windows.net/{tenant id}/",
  "oid": "{enterprise app object id}",
  "sub": "{enterprise app object id}",
  "tenant_region_scope": "NA",
  "tid": "{tenant id}",
  "uti": "uIzrJNpHcEGXoQ4ZKZgqAA",
  "ver": "1.0"
}

{
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure.",
    "innerError": {
      "request-id": "3537d28e-a061-4430-aef5-4a75bf791d90",
      "date": "2017-09-07T16:38:26"
    }
  }
}

我已确保应用程序具有通过门户分配的正确权限。在Required Permissions > Application Permissions下,选择“Read and write all groups”。

Azure Portal Permissions

我有什么遗漏或者这不可能吗?

【问题讨论】:

  • 您能否在jwt.io 之类的服务中检查 JWT,并确保它包含具有必要权限的 roles 声明?
  • JWT 中似乎没有列出任何角色。在这种情况下,我需要在请求中添加什么内容吗?
  • 您是否在设置应用权限要求的地方按下了授予权限按钮?此外,请确保您请求权限的是 Microsoft Graph API。
  • 是的,我在点击授予权限前后都试过了。我还验证了 AP​​I 是 Microsoft Graph。我添加了指向我的权限设置屏幕截图的链接。
  • 你能分享一下 JWT 令牌吗? (删除任何敏感属性)如果缺少角色声明是一个问题,我希望错误是“权限不足”。此错误意味着令牌本身有问题。

标签: azure-active-directory microsoft-graph-api


【解决方案1】:

在您的 JWT 令牌中,受众值 (aud) 错误。

如果您尝试调用 https://graph.microsoft.com 或其任何 API,则需要一个带有 aud 声明的令牌,该声明为 https://graph.microsoft.com00000003-0000-0000-c000-000000000000

您拥有的令牌用于 AAD Graph API,https://graph.windows.net a.k.a. 00000002-0000-0000-c000-000000000000

虽然这两个资源在 URL 和 GUID 形式中看起来很相似,但它们是完全不同的身份。您应该在整个代码中确认您在检索访问令牌时指定了正确的资源值。您上面的小样本表明您做对了,但令牌表明您不是。

【讨论】:

  • 你是对的。我发送的是 scope=graph.microsoft.com,而不是像我说的那样发送 resource=graph.microsoft.com。我发誓我已经尝试过这两种方法,但它现在正在工作。可能是我直到现在点击授予权限后才尝试过这种方式。谢谢。
  • 很高兴这解决了您的问题。请随意接受我的回答作为正确答案,以便其他人知道此问题已解决。
猜你喜欢
  • 2020-02-28
  • 2018-04-11
  • 1970-01-01
  • 2020-12-25
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 2020-03-25
相关资源
最近更新 更多