diff --git a/sdk/identity/azure-identity/azure/identity/_internal/token_binding_transport_requests.py b/sdk/identity/azure-identity/azure/identity/_internal/token_binding_transport_requests.py index 19d15abae1d9..aec49ae1f825 100644 --- a/sdk/identity/azure-identity/azure/identity/_internal/token_binding_transport_requests.py +++ b/sdk/identity/azure-identity/azure/identity/_internal/token_binding_transport_requests.py @@ -8,8 +8,8 @@ import ssl from typing import Any, Optional -from requests.adapters import HTTPAdapter -from requests import Session +from requests.adapters import HTTPAdapter # pylint: disable=networking-import-outside-azure-core-transport +from requests import Session # pylint: disable=networking-import-outside-azure-core-transport from azure.core.pipeline.transport import ( # pylint: disable=non-abstract-transport-import, no-name-in-module RequestsTransport, ) diff --git a/sdk/identity/azure-identity/azure/identity/aio/_internal/token_binding_transport_asyncio.py b/sdk/identity/azure-identity/azure/identity/aio/_internal/token_binding_transport_asyncio.py index fa236c2e4d4f..6aa02259c7f9 100644 --- a/sdk/identity/azure-identity/azure/identity/aio/_internal/token_binding_transport_asyncio.py +++ b/sdk/identity/azure-identity/azure/identity/aio/_internal/token_binding_transport_asyncio.py @@ -7,7 +7,7 @@ """ from typing import Any, Optional -from requests import Session +from requests import Session # pylint: disable=networking-import-outside-azure-core-transport from azure.core.pipeline.transport import ( # pylint: disable=non-abstract-transport-import, no-name-in-module AsyncioRequestsTransport, ) diff --git a/sdk/identity/azure-identity/samples/custom_credentials.py b/sdk/identity/azure-identity/samples/custom_credentials.py index 6177a4dbf0a7..4c6fcbae655e 100644 --- a/sdk/identity/azure-identity/samples/custom_credentials.py +++ b/sdk/identity/azure-identity/samples/custom_credentials.py @@ -7,9 +7,10 @@ import time from typing import Optional, Union +import msal + from azure.core.credentials import AccessToken from azure.identity import AuthenticationRequiredError, AzureAuthorityHosts -import msal class StaticTokenCredential(object): @@ -30,7 +31,14 @@ def __init__(self, access_token: Union[str, AccessToken]) -> None: def get_token( self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs ) -> AccessToken: - """get_token is the only method a credential must implement""" + """Get an access token for the requested scopes. + + :param str scopes: desired scopes for the access token. + :keyword str claims: additional claims required in the token request. + :keyword str tenant_id: optional tenant to include in the token request. + :return: An access token with the requested scopes. + :rtype: ~azure.core.credentials.AccessToken + """ return self._token @@ -46,13 +54,20 @@ def __init__(self, tenant_id: str, client_id: str) -> None: def get_token( self, *scopes: str, claims: Optional[str] = None, tenant_id: Optional[str] = None, **kwargs ) -> AccessToken: - """get_token is the only method a credential must implement""" + """Get an access token for the requested scopes. + + :param str scopes: desired scopes for the access token. + :keyword str claims: additional claims required in the token request. + :keyword str tenant_id: optional tenant to include in the token request. + :return: An access token with the requested scopes. + :rtype: ~azure.core.credentials.AccessToken + """ now = int(time.time()) result = self._app.acquire_token_interactive(list(scopes), claims=claims, tenant_id=tenant_id, **kwargs) try: return AccessToken(result["access_token"], now + int(result["expires_in"])) - except: + except Exception as ex: print("\nFailed to get a valid access token") - raise AuthenticationRequiredError(scopes) + raise AuthenticationRequiredError(scopes) from ex