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

Unable to authenticate the ModernUsageDetail api #35537

Closed
Gopichand-Heeddata opened this issue May 8, 2024 · 3 comments
Closed

Unable to authenticate the ModernUsageDetail api #35537

Gopichand-Heeddata opened this issue May 8, 2024 · 3 comments
Assignees
Labels
Azure.Identity customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@Gopichand-Heeddata
Copy link

Gopichand-Heeddata commented May 8, 2024

I used the SubscriptionClient and ClientSecretCrerdentials to authenticate the azure APIs.
When I want to create the bearer token to modernusage API to get 1-year data, I am not able to generate the bearer token. will you please suggest any procedure to create that token.
I am attaching the implemented code:
@Property
def subscription(self):
if self._subscription:
return self._subscription
self._subscription = SubscriptionClient(
self.service_principal_credentials)
return self._subscription
@Property
def service_principal_credentials(self):
if self._service_principal_credentials:
return self._service_principal_credentials
self._service_principal_credentials = (
self._get_service_principal_credentials())
return self._service_principal_credentials

 def _get_service_principal_credentials(self):
    try:
        credentials = ClientSecretCredential(
            client_id=self.config['client_id'],
            client_secret=self.config['secret'],
            tenant_id=self.config['tenant'],
        )
    except AuthenticationError as ex:
        description = self._get_error_description(ex)
        raise AuthorizationException(description)
    return credentials
@property
def raw_client(self):
    return self.subscription._client
def get_modern_usage(self, start_date, range_end=None, limit=None):
    """Get priced usage for current 'modern' subscription.
    UsageDetails API doesn't filter data for daterange in python SDK
    for 'modern' subscriptions.
    :param start_date: start datetime
    :param range_end: end datetime
    :param limit: result limit
    :return: an iterator with usage objects
    """
    date_format = '%Y-%m-%d'
    start_str = start_date.strftime(date_format)
    end_str = range_end.strftime(date_format)
    scope = 'subscriptions/{}'.format(self._subscription_id)
    base_url = 'https://management.azure.com'
    usage_url = self.consumption.usage_details.list.metadata[
        'url'].format(scope=scope)
    url = f'{base_url}{usage_url}?$extend=properties/meterDetails,' \
          f'properties/additionalProperties&startDate={start_str}' \
          f'&endDate={end_str}&api-version=2021-10-01'
    if limit:
        url += f'&top={limit}'
    token = self.raw_client.config.credentials.token['access_token']
    headers = {'Authorization': f'Bearer {token}'}
    request = Request(method='GET', url=url, headers=headers)
    result = self.raw_client.send(request)
    deserialized = DESERIALIZER.deserialize_data(
        result.json(), "UsageDetailsListResult")
    next_link = deserialized.next_link
    for v in deserialized.value:
        yield v
    while next_link:
        request.url = next_link
        result = self.raw_client.send(request)
        deserialized = DESERIALIZER.deserialize_data(
            result.json(), "UsageDetailsListResult")
        next_link = deserialized.next_link
        for v in deserialized.value:
            yield v
The token in the last method is not able to create.
The used versions are:
"azure-mgmt-subscription==3.1.1",
"azure-mgmt-consumption==10.0.0",
"azure-mgmt-commerce==6.0.0",
"msrest==0.7.1",
"msrestazure==0.6.4",
"urllib3>=1.25.3",
"azure-identity==1.15.0",

@shanselman @osake @notlaforge can you please provide me solution.

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels May 8, 2024
@xiangyan99 xiangyan99 added Azure.Identity and removed needs-triage This is a new issue that needs to be triaged to the appropriate team. labels May 8, 2024
@xiangyan99 xiangyan99 self-assigned this May 8, 2024
@github-actions github-actions bot added the needs-team-attention This issue needs attention from Azure service team or SDK team label May 8, 2024
@xiangyan99
Copy link
Member

Thanks for reaching out.

In your code, seems like you only create the credential object but do not call get_token method.

You can get more details in https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/azure/identity/_credentials/client_secret.py.

@xiangyan99 xiangyan99 added the issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. label May 8, 2024
@github-actions github-actions bot removed the needs-team-attention This issue needs attention from Azure service team or SDK team label May 8, 2024
Copy link

github-actions bot commented May 8, 2024

Hi @Gopichand-Heeddata. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

Copy link

Hi @Gopichand-Heeddata, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Identity customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
Development

No branches or pull requests

2 participants