使用azure api列出所有区域

时间:2017-01-09 08:13:39

标签: rest azure

我正在尝试使用azure rest api获取可以配置vm的所有可能区域。 我发现的终点是

https://management.core.windows.net/<subscription-id>/locations

记录在案here

但我收到了错误。

<Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>ForbiddenError</Code><Message>The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.</Message></Error>

此外,我尝试使用查询参数?api-version=2016-04-01和标题x-ms-version: 2016-04-01,但没有任何运气。

2 个答案:

答案 0 :(得分:1)

根据您的错误代码ForbiddenError,标题中似乎没有授权或不正确的授权。如果是这种情况,请尝试在标题中添加授权。授权格式以“ Bearer ”开头,如“Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiI ......”。有关如何获取访问令牌的更多信息,请参阅document。它对我有用,我使用x-ms-version: 2014-10-01,详情请参考快照。

enter image description here

答案 1 :(得分:0)

您正在使用古老的管理端点 这是ARM的查询(使用ARMClient):

C:\>armclient.exe get https://management.azure.com/subscriptions/
        xxxxx-xxxxx-xxxxx-xxxxx/locations?api-version=2016-09-01 -verbose

---------- Request -----------------------

GET /subscriptions/xxxx-xxxx-xxxxx-xxxxx/locations?api-version=2016-09-01 HTTP/1.1
Host: management.azure.com
Authorization: Bearer eyJ0eXAiOiJKV...
User-Agent: ARMClient/1.1.1.0
Accept: application/json
x-ms-request-id: xxxxxxxxxxxx


---------- Response (326 ms) ------------

HTTP/1.1 200 OK
Pragma: no-cache
x-ms-ratelimit-remaining-subscription-reads: 14999
x-ms-request-id: xxxxxxxxxxxxx
x-ms-correlation-request-id: xxxxxxxxxxx
x-ms-routing-request-id: NORTHEUROPE:20170109T094615Z:exxxxxxxxxxxxxx
Strict-Transport-Security: max-age=31536000; includeSubDomains
Cache-Control: no-cache
Date: Mon, 09 Jan 2017 09:46:15 GMT

{
  "value": [
    {
      "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/locations/eastasia",
      "name": "eastasia",
      "displayName": "East Asia",
      "longitude": "114.188",
      "latitude": "22.267"
    },
    {
      "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/locations/southeastasia",
      "name": "southeastasia",
      "displayName": "Southeast Asia",
      "longitude": "103.833",
      "latitude": "1.283"
    },
    {
      "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/locations/centralus",
      "name": "centralus",
      "displayName": "Central US",
      "longitude": "-93.6208",
      "latitude": "41.5908"
    },
    {
      "id": "/subscriptions/xxxx-xxxx-xxxx-xxxx/locations/eastus",
      "name": "eastus",
      "displayName": "East US",
      "longitude": "-79.8164",
      "latitude": "37.3719"
    },
...

如果您有意查询旧的RDFE管理API,则需要在通话中包含管理证书。

我认为您也可以使用带RDFE的令牌,但不能使用Bearer令牌。只需嗅一下PowerShell正在做的Get-AzureLocation -debug