繁体   English   中英

如何通过https://management.azure.com api进行身份验证?

[英]How to authenticate for https://management.azure.com api?

我想通过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

当我运行此脚本时,我得到:

远程服务器返回错误:(401)未经授权。

当我导航到URL时,我得到:

error: {
    code: "AuthenticationFailed",
    message: "Authentication failed. The Authorization header is missing."
}

我认为问题是我无法使用$env:System_AccessToken令牌访问management api 但是我找不到需要哪种身份验证的信息。

如错误所述,授权标头不正确。

$URI = "https://management.azure.com/providers/microsoft.resources/checkresourcename?api-version=2014-01-01"
Invoke-RestMethod -Uri $URI -Method GET -Headers $authHeader

您可以使用两种方法来创建标题:

  1. 如您所述-Azure Powershell检查资源名称

  2. 通过创建Bearer令牌: Powershell脚本删除Azure中未使用的资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM