0

REST API を使用して、Azure 消費情報のダウンロードを自動化しようとしています。
サブスクリプションのリストを返すサンプルを修正しました。
URL を変更し、いくつかのパラメーターを追加しました。

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.

同じトークンを使用して、すべてのサブスクリプションのリストを取得できます。

4

1 に答える 1