Error 404 when using Price Sheets API

Gustavo Assis 20 Reputation points
2024-07-25T18:47:30.96+00:00

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)

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
3,010 questions
{count} votes

Accepted answer
  1. SadiqhAhmed-MSFT 48,351 Reputation points Microsoft Employee
    2024-07-30T16:49:26.4533333+00:00

    @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

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.