尝试使用使用 Azure 广告身份验证部署和保护的不记名令牌执行 API 调用时获取 401

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

我正在尝试使用 powershell 创建 runbook,同时使用调用 webrequest 调用 api(部署在 azure 中),我在 headers 中传递访问令牌,但我没有获得身份验证。

$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 的广告应用程序 -> Expose an API -> 获取 Application ID URI(如果没有设置,点击Set进行设置)。

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

$MapUrl="Application ID URI"