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

我正在尝试使用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"
    }
  }
}
ldxzzm 回答:在Powershell中使用从AzureRmContext获取的令牌后,AD图API的访问令牌验证失败

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

本文链接:https://www.f2er.com/3164584.html

大家都在问