I want to fetch the pricesheet for the Azure items on my subscription, considering any potential discounts. I understood from the API documentation, that using the billing period method would return the prices. The authentication works well, but when it hits the API after authenticating I get an error 404 back. What am I doing wrong?
API Documentation: https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet?view=rest-consumption-2023-05-01
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)
Thank you!
I tried changing the API endpoints, double checked my subscription activation, allowed API access and assigned role to the app service created. Double checked tenant, client etc. IDs…
guzassis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.