簡體   English   中英

嘗試使用承載令牌執行 API 調用時獲取 401,承載令牌部署並受 Azure 廣告身份驗證保護

[英]Getting 401 while trying perform API call with bearer token which is deployed and protected with Azure Ad Authentication

我嘗試使用 powershell 創建運行手冊,同時使用調用 webrequest 調用 api(部署在 azure 中),我在標頭中傳遞訪問令牌並且我沒有獲得身份驗證。

$TenantId="xyxyxyx"
$MapUrl="https://graph.microsoft.com"
$Authority="https://login.microsoftonline.com/$TenantId"
$ClientSecret="secret"
$AuthContext=New-Object "Micorsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -argumentlist $Authority
$ClientCredential=New-Object "Micorsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential -argumentlsit $ClientId,$ClientSecret
$ResultToken=$AuthContext.AcquireTokenAsync($MapUrl,$ClientCredential).Result


By Using the above steps i got the access token then im using the token in the request like this

Invoke-WebRequest -uri 'https://sssss/api/Get' -Headers @{"Authorization"="(Bearer $ResultToken.AccessToken)"}


I dont know what im missing can someone help me with this. Thanks in Advance

您的腳本獲取 Microsoft Graph API 的訪問令牌,要調用您的自定義 API,您需要獲取代表您的 API 的 AD 應用程序的訪問令牌。

導航到門戶 -> Azure Active Directory -> App registrations -> 找到代表您的 API 的 AD 應用程序 -> Expose an API -> 如果沒有Set它,請單擊Application ID URI .

在此處輸入圖像描述

然后在您的腳本中,用它更改$MapUrl

$MapUrl="Application ID URI"

暫無
暫無

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

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