Getting cost details through REST API

Kumar, Arun 336 Reputation points
2023-08-29T22:06:46.61+00:00

Hi,

I am trying to get the cost details of subscription using below POST in Python code

https://management.azure.com//subscriptions/1234/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version=2023-03-01

Getting the below error

<Response [400]> {'error': {'code': '400', 'message': 'Invalid request payload, Invalid member :grant_type

I am passing the following data details in the post request. Is this correct? anything i am doing wrong?

"grant_type": "client_credentials",
"client_id":  "1234",
"client_secret": "1234",
"scope": "https://management.azure.com/.default"


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

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 24,531 Reputation points
    2023-08-30T11:17:51.6066667+00:00

    I think you are mixing up Azure authentication with the actual API request for cost details. The error indicates that the payload you provided is incorrect for the generateCostDetailsReport endpoint.

    Before making the actual API call to get the cost details, you must first acquire an access token using the Azure AD endpoint.

    Once you have the access token, you can use it in the Authorization header to call the Azure Management API to get cost details.

    Let me break it down for you:

    1. Acquire the access token:

    Here's how you can get the access token using your client details:

    
    import requests
    
    # Set up your details
    
    tenant_id = '<your-tenant-id>'
    
    client_id = '1234'  # Replace with your client ID
    
    client_secret = '1234'  # Replace with your client secret
    
    # URL to get the token
    
    url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/token'
    
    # Request body
    
    data = {
    
        'grant_type': 'client_credentials',
    
        'client_id': client_id,
    
        'client_secret': client_secret,
    
        'resource': 'https://management.azure.com/'
    
    }
    
    # Post request to get the access token
    
    response = requests.post(url, data=data)
    
    token = response.json().get('access_token')
    
    
    1. Use the acquired access token:

    Now that you have the token, you can call the generateCostDetailsReport endpoint:

    
    subscription_id = '1234'  # Your subscription ID
    
    api_version = '2023-03-01'
    
    cost_url = f'https://management.azure.com/subscriptions/{subscription_id}/providers/Microsoft.CostManagement/generateCostDetailsReport?api-version={api_version}'
    
    headers = {
    
        'Authorization': f'Bearer {token}',
    
        'Content-Type': 'application/json'
    
    }
    
    # Here, you might have to pass some payload specific to the generateCostDetailsReport API
    
    response = requests.post(cost_url, headers=headers)
    
    print(response.json())
    
    
    1 person found this answer 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.