簡體   English   中英

使用從 Powershell 中的 AzureRmContext 獲取的令牌后,AD 圖 API 的訪問令牌驗證失敗

[英]Access token validation failure for AD graph API after using the token acquired from AzureRmContext in Powershell

我正在嘗試使用 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"
    }
  }
}

查看https://jwt.ms中的令牌,看看 aud 聲明是什么。 我認為您通過 AzureRm 獲得的令牌是對 Azure 管理 API 的訪問令牌。 MS Graph 的值為“ https://graph.microsoft.com ”。 您可以使用 AzureAD PS 模塊來獲取 Graph 令牌。 另請注意,AAD 與 MS Graph 不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM