使用从Powershell中的AzureRmContext获取的令牌后,AD图API的访问令牌验证失败

问题描述 投票:0回答:1

我正在尝试使用Oauth2登录到Microsoft graph API的API调用。

我曾尝试使用AzureRm cmdlet来获取我的帐户的令牌,因此可以进行API调用,但显示消息“访问令牌验证失败。无效的受众群体。”显示在JSON响应中。

Login-AzureRmAccount

$currentAzureContext = Get-AzureRmContext
$tenantId = $currentAzureContext.Tenant.Id
$accountId = $currentAzureContext.Account.Id

$tokenCache = $currentAzureContext.TokenCache
$cachedTokens = $tokenCache.ReadItems() `
        | where { $_.TenantId -eq $tenantId }

$accessToken = $cachedTokens.AccessToken
Invoke-RestMethod -Method Get `
                 -Uri ("https://graph.microsoft.com/v1.0/me") `
                -Headers @{ "Authorization" = "Bearer " + $accessToken }

以下是JSON响应:

Invoke-RestMethod : {
  "error": {
    "code": "InvalidAuthenticationToken",
    "message": "Access token validation failure. Invalid audience.",
    "innerError": {
      "request-id": "8429e520-401b-4382-adad-4f55bccbe752",
      "date": "2019-11-04T16:53:27"
    }
  }
}
azure powershell azure-active-directory azure-ad-graph-api
1个回答
1
投票

[查看https://jwt.ms中的令牌,并了解什么是aud声明。我认为您通过AzureRm获得的令牌是对Azure管理API的访问令牌。 MS Graph的值为“ https://graph.microsoft.com”。您可以使用AzureAD PS模块获取图形令牌。另请注意,AAD与MS Graph不同。

© www.soinside.com 2019 - 2024. All rights reserved.