使用 REST API 获取 Azure API 管理 SKU

Get Azure API Management SKU using REST API

我正在尝试使用 REST API 获取 api 管理 sku 条件,但由于发生 401 未授权错误而无法正常工作。但是,我通过尝试执行其他 API 管理 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."}}

您在 header 代币中输了 Bearer

-H "Authorization: Bearer $SAS"

还有,你用的api是一个Azure Rest API. The token is not your generate by APIM Publisher portal. You need create a service principal,那就用它来获取token吧。例如:

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/"

获得token后,可以使用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"