我正在尝试使用 REST API 自动下载 Azure 消费信息。
我修改了返回订阅列表的示例。
我更改了网址并添加了几个附加参数。
import adal
import requests
authentication_endpoint = 'https://login.microsoftonline.com/'
resource = 'https://management.core.windows.net/'
application_id = '4c681786-980d-44cb-89f9-123456789012'
application_secret = 'xxxxxxxxxxxxxxxxxxxxxxx'
tenant_id = 'xxxxxxxxx-xxxx-xxxx-xxxx-123456789012'
enrollmentNumber = 'XXXXXXXX'
# get an Azure access token using the adal library
context = adal.AuthenticationContext(authentication_endpoint + tenant_id)
token_response = context.acquire_token_with_client_credentials(resource, application_id, application_secret)
access_token = token_response.get('accessToken')
# endpoint = 'https://management.azure.com/subscriptions/?api-version=2015-01-01'
scope = '/providers/Microsoft.Billing/enrollmentAccounts/' + enrollmentNumber
endpoint = 'https://management.azure.com' + scope + '/providers/Microsoft.Consumption/usageDetails?api-version=2019-10-01'
headers = {"Authorization": 'Bearer ' + access_token}
json_output = requests.get(endpoint,headers=headers).json()
print(json_output)
我收到一个错误,而不是带有消费信息的 json:
Puid is null/empty. Puid must be present in the header for user to get authorized.
使用相同的令牌,我可以获得所有订阅的列表。