azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

the error "(401) Unauthorized" occurs when try to iterate from the results of comsumption.usage_details.list()

Open imeya opened this issue 7 years ago • 9 comments

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:

mgmt-auth-error

Please let me know if I misunderstand or miss something. For authentication, I followed this doc.

imeya avatar Feb 14 '19 06:02 imeya

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 avatar Feb 15 '19 18:02 lmazuel

@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?

imeya avatar Feb 16 '19 03:02 imeya

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

darknode avatar Feb 20 '19 13:02 darknode

@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.

lmazuel avatar Mar 01 '19 17:03 lmazuel

Getting same 401 error, when calling get/create method on budgets. This is an intermittent error.

AmudhaPalani avatar Mar 14 '19 14:03 AmudhaPalani

still happens for me, appears to only happen when retrieving data from over 12 months ago, and using a skipToken param

williamthom-as avatar Apr 08 '19 03:04 williamthom-as

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)

faisquinha avatar May 15 '20 09:05 faisquinha

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

ms-premp avatar Jun 28 '20 09:06 ms-premp

Closing per above.

ramaganesan-rg avatar Jun 29 '20 00:06 ramaganesan-rg