azure - 如何对 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
. 但我找不到需要哪种身份验证的信息。
解决方案
如错误所述,授权标头不正确。
$URI = "https://management.azure.com/providers/microsoft.resources/checkresourcename?api-version=2014-01-01"
Invoke-RestMethod -Uri $URI -Method GET -Headers $authHeader
您可以使用几种方法来创建标题:
正如您所提到的 - Azure Powershell 检查资源名称
通过创建 Bearer 令牌:Powershell 脚本来删除 Azure 中未使用的资源
推荐阅读
- powershell - Powershell:用于验证输入的 GUI
- java - 为什么这个循环需要递减?
- kubernetes - 重启 kubelet 会停止所有节点吗?
- mysql - Sequelize.fn(`ANY_VALUE`, sequelize.col(`col`)); 不返回数据
- java - Java DayOfWeek 将日期名称(字符串)转换为日期编号
- c++ - 指向折叠表达式中的成员函数的指针
- r - 在 r 脚本中使用 Readline 值查找和替换 ALL
- reactjs - 如何将事件对象传递给打字稿中的 onchange 处理程序?
- wordpress - 在特定 URL 上禁用 SSL
- python - How to export results using Python in Google Earth Engine