首页 > 解决方案 > 如何对 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) Unauthorized。

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

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

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

标签: azurepowershell

解决方案


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

$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 中未使用的资源


推荐阅读