[英]How to authenticate for https://management.azure.com api?
I want to retrieve data on my DNS zones through a API call: 我想通过API调用检索DNS区域上的数据:
$api = "?api-version=2018-05-01"
$pat = "Bearer $env:System_AccessToken"
Write-Host "### PAT ###"
Write-Host $pat
$DNSInformation = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Network/dnsZones/$zoneName/$recordType/$relativeRecordSetName$api"
Write-Host "###"
Write-Host $DNSInformation
Write-Host "###"
$x = Invoke-RestMethod -Uri $DNSInformation -Headers @{Authorization = $pat } -Method Get
When I run this script I get: 当我运行此脚本时,我得到:
The remote server returned an error: (401) Unauthorized. 远程服务器返回错误:(401)未经授权。
When I navigate to the URL I get: 当我导航到URL时,我得到:
error: {
code: "AuthenticationFailed",
message: "Authentication failed. The Authorization header is missing."
}
I think the issue is that I can't use the $env:System_AccessToken
token to get on the management api
. 我认为问题是我无法使用$env:System_AccessToken
令牌访问management api
。 But I can't find information what kind of authentication is needed. 但是我找不到需要哪种身份验证的信息。
As the error mentions, the authorization header is incorrect. 如错误所述,授权标头不正确。
$URI = "https://management.azure.com/providers/microsoft.resources/checkresourcename?api-version=2014-01-01"
Invoke-RestMethod -Uri $URI -Method GET -Headers $authHeader
You can use a couple of approaches to create your header: 您可以使用两种方法来创建标题:
As you mentioned - Azure Powershell to check resource names 如您所述-Azure Powershell检查资源名称
By creating Bearer token : Powershell Script to delete unused resources in Azure 通过创建Bearer令牌: Powershell脚本删除Azure中未使用的资源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.