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

时间:2017-09-07 17:18:58

标签: azure-active-directory microsoft-graph

是否可以使用通过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"
    }
  }
}

我确保应用程序具有通过门户网站分配的正确权限。在“必需的权限”下>应用程序权限,"读写所有组"被选中。

Azure Portal Permissions

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

1 个答案:

答案 0 :(得分:3)

在您的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形式中看起来相似,但它们是完全独立的身份。您应该在整个代码中确认在检索访问令牌时指定了正确的资源值。上面的小样本暗示你做得正确,但令牌表明你没有。