Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
imeya opened this issue Feb 14, 2019 · 9 comments
Assignees
Labels
Mgmt This issue is related to a management-plane library. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@imeya
Copy link

imeya commented Feb 14, 2019

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.

@lmazuel
Copy link
Member

lmazuel commented Feb 15, 2019

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 lmazuel added question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Consumption Service Attention Workflow: This issue is responsible by Azure service team. Mgmt This issue is related to a management-plane library. labels Feb 15, 2019
@imeya
Copy link
Author

imeya commented Feb 16, 2019

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

@darknode
Copy link

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

@lmazuel
Copy link
Member

lmazuel commented Mar 1, 2019

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

@AmudhaPalani
Copy link

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

@williamthom-as
Copy link

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

@faisquinha
Copy link

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)

@ms-premp
Copy link

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

@ramaganesan-rg
Copy link
Member

Closing per above.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Mgmt This issue is related to a management-plane library. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

9 participants