使用REST API获取Azure API Management SKU

时间:2018-01-29 07:28:57

标签: rest api azure azure-api-management azure-cli

我正在尝试使用REST API获取api management sku条件,但是由于发生401 Unauthorized错误而无法正常工作。但是,我通过尝试执行其他API Management REST API来验证SAS是否有效。 我不知道,请帮助我。

* API

URL=https://management.azure.com/subscriptions/mysubid/resourceGroups/myrg/providers/Microsoft.ApiManagement/service/myservice
API_VER=2017-03-01
SAS="**Generate by APIM Publisher portal**"

curl -i GET "$URL?api-version=$API_VER" -H "Authorization: $SAS"

*错误

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
WWW-Authenticate: Bearer authorization_uri="https://login.windows.net/4cb021a3-bd70-42f3-91ef-******", error="invalid_token", error_description="The authentication scheme of SharedAccessSignature is not supported."
x-ms-failure-cause: gateway
x-ms-request-id: 2398890d-aeea-4580-******
x-ms-correlation-request-id: 2398890d-aeea-4580-b85b-******
x-ms-routing-request-id: JAPANWEST:20180129T071135Z:2398890d-aeea-4580-b85b-*******
Strict-Transport-Security: max-age=31536000; includeSubDomains
Date: Mon, 29 Jan 2018 07:11:35 GMT
Connection: close
Content-Length: 150

{"error":{"code":"AuthenticationFailedInvalidHeader","message":"Authentication failed. The 'Authorization' header is provided in an invalid format."}}

1 个答案:

答案 0 :(得分:0)

您的标头令牌中丢失了Bearer

-H "Authorization: Bearer $SAS"

此外,您使用的API是here。该令牌不是由APIM Publisher门户生成的。您需要创建一个Azure Rest API,然后使用它来获取令牌。例如:

TENANTID=""
APPID=""
PASSWORD=""
curl -X "POST" "https://login.microsoftonline.com/$TENANTID/oauth2/token" \
-H "Cookie: flight-uxoptin=true; stsservicecookie=ests; x-ms-gateway-slice=productionb; stsservicecookie=ests" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$APPID" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_secret=$PASSWORD" \
--data-urlencode "resource=https://management.azure.com/"

获得令牌后,您可以使用API​​获取sku。

curl -X "GET" "https://management.azure.com/subscriptions/*********/resourceGroups/shuiapi/providers/Microsoft.ApiManagement/service/shuiapi?api-version=2017-03-01" \
    -H "Authorization: Bearer $token" \
    -H "Content-Type: application/json"