Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,354 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I am trying to fetch the pricing for my subscription using the Price Sheets API endpoint and the documentation provided. I created a Python code meant to fetch the data, as below, but I keep getting Error 404 (I get past authentication successfully). What I am doing wrong?
import requests
# Define your Azure AD and API parameters
tenant_id = 'xxx'
client_id = 'yyy'
client_secret = 'zzz'
subscriptionId = 'www'
resource = 'https://management.azure.com/'
billingPeriodName = "202407"
# OAuth2 endpoint for token retrieval
token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
# Request headers and body for token retrieval
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://management.azure.com/.default'
}
# Get the access token
response = requests.post(token_url, data=token_data)
response.raise_for_status()
access_token = response.json().get('access_token')
# Endpoint to retrieve the current retail prices
retail_prices_url = f'https://management.azure.com/subscriptions/{subscriptionId}/providers/Microsoft.Billing/billingPeriods/{billingPeriodName}/providers/Microsoft.Consumption/pricesheets/default?api-version=2023-05-01'
# Request headers for the API call
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Get the current retail prices
response = requests.get(retail_prices_url, headers=headers)
response.raise_for_status()
retail_prices_data = response.json()
response.raise_for_status() # Raise an error for bad status codes
print(token_data)
print(pricesheet_data)