@Gustavo Assis Greetings!
Thank you for providing your Python code. Based on the code you provided, it looks like you are using the correct API endpoint and version, and you are authenticating correctly using Azure AD.
The issue may be related to the subscription ID or billing period name you are using. Make sure that the subscription ID and billing period name you are using are correct. You can find the subscription ID in the Azure portal, and the billing period name can be found in the Azure documentation. Reference link - https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet/get-by-billing-period?view=rest-consumption-2023-05-01&tabs=HTTP&tryIt=true&source=docs#code-try-0
Also, make sure that the account you are using to authenticate has the necessary permissions to access the Price Sheets API endpoint. You can check the permissions in the Azure portal.
Confirm that the access token is valid and has not expired. An invalid token can lead to a 404 error.
Make sure that the necessary resource providers, such as Microsoft.Billing
and Microsoft.Consumption
, are registered in your Azure subscription.
Finally, you are printing token_data
and pricesheet_data
, but pricesheet_data
is not defined in your code. You may want to change the last line to print(retail_prices_data)
to print the retail prices data.
Here's a slightly adjusted version of your Python code with some modifications:
import requests
# Define your Azure AD and API parameters
tenant_id = 'xxx'
client_id = 'yyy'
client_secret = 'zzz'
billingPeriodName = "202407" # Ensure this is a valid billing period
# 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/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)
# Check for errors
if response.status_code == 404:
print(f"Error 404: Resource not found. Verify the API endpoint and billing period.")
else:
response.raise_for_status()
retail_prices_data = response.json()
print(retail_prices_data)
EDIT: Product team has confirmed that this API - https://learn.microsoft.com/en-us/rest/api/consumption/price-sheet/get-by-billing-period?view=rest-consumption-2023-05-01&tabs=HTTP&tryIt=true&source=docs#code-try-0 does not support MCA and only works with Enterprise Agreements.
Hope this helps!
If the response helped, do "Accept Answer" and up-vote it