Azure/azure-sdk-for-python

Unable to authenticate the ModernUsageDetail api

Gopichand-Heeddata opened this issue · 3 comments

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.

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.

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.

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.