the error "(401) Unauthorized" occurs when try to iterate from the results of comsumption.usage_details.list()
I'm trying to use ConsumptionManagementClient to get the usage details with a date_filter, but receive the following error: azure.mgmt.consumption.models.error_response.ErrorResponseException: (401) Unauthorized.
My code as below(the same credentials works well for vm usage):
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.consumption import ConsumptionManagementClient
# Tenant ID for your Azure Subscription
TENANT_ID = 'xxx'
# Your Service Principal App ID
CLIENT = 'xxx'
# Your Service Principal Password
KEY = 'xxx'
credentials = ServicePrincipalCredentials(
client_id = CLIENT,
secret = KEY,
tenant = TENANT_ID
)
subscription_id = 'xxx'
comsumption = ConsumptionManagementClient(credentials,subscription_id,base_url=None)
startDate='2019-01-01T00:00:00Z'
date_filter = 'usageStart ge ' + str(startDate)
for tt in list(comsumption.usage_details.list(filter=date_filter)):
print(tt.usage_start)
The screenshot of error message:
Please let me know if I misunderstand or miss something. For authentication, I followed this doc.
Hi @imeya Consumption requires special permissions level that likely your current SP doesn't have. For instance the billing reader role: https://docs.microsoft.com/en-us/azure/role-based-access-control/built-in-roles#billing-reader
Please update your SP permissions level to this role, and let us know if your problem is solved.
@lmazuel , thank you very much.
do you mind that let me how do date filter in the code below:
date_filter = 'usageStart ge 2019-01-01T00:00:00Z'
for tt in list(comsumption.usage_details.list(filter=date_filter)):
print(tt.usage_start)
Seams that the filter does not respect the date_filter, is there something wrong with my date_filter?
Hi @lmazuel ! I have almost the same issue. Service principal/application has Reader and Billing Reader roles assigned. Example code:
billing_period_name = "201903-1"
consumption_client = consumption.ConsumptionManagementClient(credentials, subscription_id)
pages = consumption_client.usage_details.list_by_billing_period(
billing_period_name,
expand="additionalProperties,meterDetails"
)
for page in pages:
print(page.instance_id)
It prints a lot of lines, but at the end raises error:
raise models.ErrorResponseException(self._deserialize, response)
azure.mgmt.consumption.models.error_response.ErrorResponseException: (401) Request is missing a certificate
@imeya from the unittests written by the service team in charge of this service:
output = list(self.consumption_client.usage_details.list_by_billing_period(
billing_period_name=MgmtConsumptionTest.billingPeriodName,
expand='properties/meterDetails,properties/additionalProperties',
filter='usageEnd eq 2017-10-26T23:59:59Z'
))
Adding @kjeur from the team to help for details and authentication question.
Getting same 401 error, when calling get/create method on budgets. This is an intermittent error.
still happens for me, appears to only happen when retrieving data from over 12 months ago, and using a skipToken param
Hello people, Is it possible to return a json? Now an object returns.
#Consumption management client provides access to consumption resources for Azure Enterprise Subscriptions consumption_client = ConsumptionManagementClient(credentials, subscription_id, base_url=None)
results = consumption_client.usage_details.list(scope = '/subscriptions/' + subscription_id +'/', expand='properties/meterDetails, properties/additionalInfo', filter="""properties/usageStart ge '2020-05-14' and properties/usageEnd le '2020-05-14'""")
for result in results: print(result)
Please try this example, we are not able to repro this issue. https://github.com/ms-premp/AzureConsumptionSamples/blob/master/Python_sdk/usage_details_list_python_sdk_example.py
Closing per above.