Python 中的 Azure 计费 API - 空响应

Azure Billing API in Python - Empty Response

我正在尝试在 Python 中构建一个(我认为会是)一个简单的脚本,以使用计费 API 下载 Microsoft 的 Azure 价目表。

token_response = adal.acquire_token_with_client_credentials(
'https://login.microsoftonline.com/' + TENANT_ID,
CLIENT-ID,
CLIENT-KEY)

access_token = token_response.get('accessToken')

endpoint = "https://management.azure.com/subscriptions/[SUBSCRIPTION-ID]/providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId eq 'MS-AZR-0003P' and Currency eq 'GBP' and Locale eq 'en-GB' and RegionInfo eq 'GB'"
headers = {"Authorization": 'Bearer ' + access_token}

out = requests.get(endpoint,headers=headers)

json_output = out.json()
print son_output

查询似乎正确执行。授权似乎没问题,我收到了 200 OK 消息响应。但输出为空:{u'value': []}。我尝试了不同的 OfferID、不同的 $filter 字符串,现在它让我抓狂...

我想知道我是否没有委派合适的权限,但我已经创建了一个附加到 Active Directory 的应用程序,生成了一个密钥,并将权限委派给了 Windows Azure 服务管理?如果那是问题所在,我不会收到错误消息吗?

很高兴听到做同样事情的其他方法,但 Python 是我真正知道的...

请确保您用于处理计费和使用情况的用户帐户 API 有权访问 Azure 订阅。在您的情况下,您使用的是 Service Principal,因此请确保为该用户分配了订阅中的角色。

要为 Service Principal 分配角色,您可能会发现此 link 有帮助:https://azure.microsoft.com/en-in/documentation/articles/resource-group-create-service-principal-portal/

在分配角色时,由于该用户只需要读取数据,因此最好使用 Reader 角色,以免无意中更改您的 Azure 订阅。