简体   繁体   English

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

[英]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: 您可以使用两种方法来创建标题:

  1. As you mentioned - Azure Powershell to check resource names 如您所述-Azure Powershell检查资源名称

  2. By creating Bearer token : Powershell Script to delete unused resources in Azure 通过创建Bearer令牌: Powershell脚本删除Azure中未使用的资源

暂无
暂无

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

相关问题 如何排除管理故障。azure.com rest Z8A5DA52ED126447D359E70C057 - How to troubleshoot an management.azure.com rest api call 简单的API调用management.azure.com - Simple API call to management.azure.com 如何解决无法查询服务端点 api:https://management.azure.com//subscriptions/ 错误-远程服务器返回错误:(403) - How to resolve Failed to query service endpoint api: https://management.azure.com//subscriptions/ Error- The remote server returned an error: (403) 此请求有什么问题(Azure management.azure.com API) - What is wrong with this request (Azure management.azure.com API) 调用管理.azure.com API认证失败 - Calling management.azure.com API fails due to authentication 如何使用 GET 请求查询 management.azure.com? - How to query management.azure.com with a GET request? 为输入参数“范围”提供的值无效。 scope 'https://management.azure.com/.default' 不存在 - The provided value for the input parameter 'scope' is not valid. The scope 'https://management.azure.com/.default' does not exist feign.RetryableException:不支持或无法识别的 SSL 消息执行 GET https://management.azure.com/subscriptions - feign.RetryableException: Unsupported or unrecognized SSL message executing GET https://management.azure.com/subscriptions 如何形成URL从管理查询Azure AD组列表。 - How to form a URL to query Azure AD group list from management.azure.com Management.Azure.com REST API 调用与 Odata $expand 和 $filter | 正文结果未过滤 - Management.Azure.com REST API Call with Odata $expand and $filter | body results not filtering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM