使用 Azure AD 1.0 终结点客户端凭据流调用 Microsoft Graph API

dal*_*asg 3 azure-active-directory microsoft-graph-api

是否可以使用通过 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
Run Code Online (Sandbox Code Playgroud)

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

解码的 JWT

标题

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "HHByKU-0DqAqMZh6ZFPd2VWaOtg",
  "kid": "HHByKU-0DqAqMZh6ZFPd2VWaOtg"
}
Run Code Online (Sandbox Code Playgroud)

有效载荷

{
  "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"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我已确保应用程序具有通过门户分配的正确权限。在必需的权限 > 应用程序权限下,选择“读取和写入所有组”。

Azure 门户权限

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

Sha*_*izi 7

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

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

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

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