I am trying to automate downloading of Azure Consumption info using REST API.
I modified sample that returns list of subscriptions.
I changed the url and I added couple of additional parameters.
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)
Instead of json with consumption info I am getting an error:
Puid is null/empty. Puid must be present in the header for user to get authorized.
With the same token, I can get list of all subscriptions.