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:
- 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')
- 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())