When using Azure Identity client library for Python, DefaultAzureCredential attempts to authenticate via the following mechanisms in this order, stopping when one succeeds:

You could set Environment Variables to fix it.

from azure.identity import DefaultAzureCredential

credential=DefaultAzureCredential()

Or set the properties in config and use ClientSecretCredential to create credential.

from azure.identity import ClientSecretCredential

subscription_id = creds_obj["AZURE_SUBSCRIPTION_ID"]
tenant_id = creds_obj["AZURE_TENANT_ID"]
client_id = creds_obj["AZURE_CLIENT_ID"]
client_secret = creds_obj["AZURE_CLIENT_SECRET"]

credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
Answer from unknown on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › python › api › overview › azure › identity-readme
Azure Identity client library for Python | Microsoft Learn
As of version 1.14.0, DefaultAzureCredential attempts to authenticate with all developer credentials until one succeeds, regardless of any errors previous developer credentials experienced. For example, a developer credential may attempt to get a token and fail, so DefaultAzureCredential will continue to the next credential in the flow.
Discussions

azure sdk python - No environment configuration found. DefaultAzureCredential() - Stack Overflow
I am trying to use this python sample to authenticate a client with an Azure Service # pip install azure-identity from azure.identity import DefaultAzureCredential # pip install azure-mgmt-compute ... More on stackoverflow.com
🌐 stackoverflow.com
azure - Validating `DefaultAzureCredential()` using Python SDK? - Stack Overflow
What are some example methods for logging and handling exceptions using try/except and DefaultAzureCredential()? Example: When DefaultAzureCredential() is used in a Python Azure Function, it genera... More on stackoverflow.com
🌐 stackoverflow.com
How to check which credential Azure python SDK class DefaultAzureCredential is using for login - Stack Overflow
I'm using Azure SDK for python in the machine learning env and to create a Workspace instance I'm using the following code. from azure.identity import DefaultAzureCredential from azureml.core import More on stackoverflow.com
🌐 stackoverflow.com
Azure Identity - DefaultAzureCredential vs AzureCliCredential
DefaultAzureCredential is the best way to develop your apps. It uses a series of different authentication methods in order to ensure you can write your code once and deploy wherever you need to. https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication-overview Highly recommend that you use this for your coding! Edit : AzureCliCredential is only used in development to authenticate your app as your user account. https://learn.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python Again - just use DefaultAzureCredential and you can authenticate with the CLI if you choose but then when you move to production it will use your app.config or configuration settings in your app service. More on reddit.com
🌐 r/AZURE
1
2
March 24, 2023
🌐
Medium
gabrielgomes61320.medium.com › microsoft-identity-library-deep-dive-e79c5d38109c
Microsoft Identity library Deep Dive | by Gabriel Gomes, PhD | Medium
December 18, 2023 - In this post, we have discussed how the Azure Identity library of the Azure SDK for Python acts to provide a handler to several possible ways of authenticating to Azure Accounts. We have seen that the DefaultAzureCredential class is very general and is able to handle multiple methods for providing OAuth Tokens to access Azure resources.
🌐
OneUptime
oneuptime.com › home › blog › how to authenticate with azure services using azure-identity
How to Authenticate with Azure Services Using azure-identity
February 16, 2026 - DefaultAzureCredential is a credential class from the azure-identity Python package. Instead of tying your code to one specific authentication method, it tries multiple methods in order until one works.
🌐
Medium
medium.com › @oliverdevijt › why-defaultazurecredential-fails-after-az-login-tenant-and-how-to-fix-it-707ac747ace0
Why DefaultAzureCredential fails after az login --tenant (and how to fix it) | by Oliver De Vijt | Medium
April 16, 2025 - When initializing DefaultAzureCredential, you can permit it to acquire tokens for additional tenants:​ · pythonfrom azure.identity import DefaultAzureCredential credential = DefaultAzureCredential(additionally_allowed_tenants=["*"])
🌐
GitHub
github.com › oneuptime › blog › tree › master › posts › 2026-02-16-authenticate-azure-services-azure-identity-defaultazurecredential-python
blog/posts/2026-02-16-authenticate-azure-services-azure-identity-defaultazurecredential-python at master · OneUptime/blog · GitHub
On your development machine, DefaultAzureCredential typically uses your Azure CLI session. Make sure you are logged in first. # Log in to Azure CLI az login # Verify your account az account show · Once logged in, any Python script using DefaultAzureCredential will automatically use your CLI session.
🌐
PyPI
pypi.org › project › azure-identity
azure-identity · PyPI
6 days ago - This is the last version to support Python 3.5. The next version will require Python 2.7 or 3.6+. AzurePowerShellCredential authenticates as the identity logged in to Azure PowerShell. This credential is part of DefaultAzureCredential by default but can be disabled by a keyword argument: DefaultAzureCredential(exclude_powershell_credential=True) (#17341)
Find elsewhere
🌐
YouTube
youtube.com › watch
11. Azure using Python SDK : What is DefaultAzureCredentials and How to use it ? - YouTube
In this video, we will cover what is DefaultAzureCredentials ? What is the benefit of it and how to use it ?Chapters :************************************0:0...
Published: June 9, 2024
🌐
Thomas Stringer
trstringer.com › authenticate-python-to-azure
Authenticate to Azure from Python | Thomas Stringer
February 28, 2021 - One of the really great and easy things that was introduced in the Azure Python SDK is the DefaultAzureCredential helper class. It is a great single solution for all of your authentication requirements because it tries five different sources of authentication.
Top answer
1 of 2
2

When using Azure Identity client library for Python, DefaultAzureCredential attempts to authenticate via the following mechanisms in this order, stopping when one succeeds:

You could set Environment Variables to fix it.

from azure.identity import DefaultAzureCredential

credential=DefaultAzureCredential()

Or set the properties in config and use ClientSecretCredential to create credential.

from azure.identity import ClientSecretCredential

subscription_id = creds_obj["AZURE_SUBSCRIPTION_ID"]
tenant_id = creds_obj["AZURE_TENANT_ID"]
client_id = creds_obj["AZURE_CLIENT_ID"]
client_secret = creds_obj["AZURE_CLIENT_SECRET"]

credential = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
2 of 2
1

I was having somewhat similar trouble following this Azure key vault tutorial which brought me here.

The solution I found was overriding the default values in the DefaultAzureCredential() constructor.

https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python

For reasons people far smarter than me will be able to explain I found that even though I had credentials from the azure cli it was not using those and instead looking for environment_credentials, which I did not have. So it threw an exception.

Once I set the exclude_environment_credential argument to True it then looked instead for the managed_identity_credentials, again which I did not have.

Eventually when I force excluded all credentials other than those from the cli it worked ok for me

I hope this helps someone. Those with more experience please feel free to edit as you see fit

Top answer
1 of 2
8

Just to supplement the accepted answer with some more details, let me add that DefaultAzureCredential does log this information. For example, if you enable INFO-level logging:

import logging
from azure.identity import DefaultAzureCredential

logger = logging.getLogger('azure.identity')
logger.setLevel(logging.INFO)

handler = logging.StreamHandler(stream=sys.stdout)
formatter = logging.Formatter('[%(levelname)s %(name)s] %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

You'll see log messages like these from DefaultAzureCredential:

[INFO] DefaultAzureCredential - EnvironmentCredential is unavailable
[INFO] DefaultAzureCredential - ManagedIdentityCredential is unavailable
[INFO] DefaultAzureCredential - SharedTokenCacheCredential is unavailable
[INFO] DefaultAzureCredential acquired a token from VisualStudioCodeCredential

You'll see the failed attempts logged only the first time the DefaultAzureCredential instance acquires a token. Once one of its constituent credentials provides a token, it uses that credential exclusively. So this particular DefaultAzureCredential will always authenticate through Visual Studio Code, and in the logs you'll see this message whenever it does so:

[INFO] DefaultAzureCredential acquired a token from VisualStudioCodeCredential
2 of 2
2

There are a few ways I might approach this but none are particularly pretty. At the risk of a naive suggestion, if you want a fine-grained understanding of what worked in sequence, you could take an approach similar to what your example sketched out using the components credentials listed here:

for credential in [EnvironmentCredential, ManagedIdentityCredential, ...]:
  try:
    credentials = credential()
    # Validate credential functionality...
    logging.info('{}.get_token succeeded'.format(credential))
  except Error1 as e1:
    logging.error('{} failed due to {}'.format(credential, e1))

I suggest this because it does not seem like what you want is exposed in an easily accessible manner, looking at the ChainedCredential source here (history is built up but only used in the log message).

More "self-contained" approaches might involve creating a custom wrapper that basically does what ChainedCredential does for DefaultAzureCredential in running the list of get_token calls, but with more ability to export the success history, you could likely hack that off of the code I linked in 2 and do something directly comparable to the DefaultAzureCredential logic, but at the end of the day it's all isomorphisms of the sample above.

Don't hesitate to shout if I've misunderstood some aspect of the question or your requirements/constraints; and as always feel free to reach out if you need to get in touch with us on our github (full disclosure, am a maintainer for some of the other Python Azure SDKs)

🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › developer › python › sdk › authentication › credential-chains
Credential chains in the Azure Identity library for Python - Python on Azure | Microsoft Learn
January 15, 2026 - To exclude all credentials except for one, set environment variable AZURE_TOKEN_CREDENTIALS to the credential name. For example, you can reduce the DefaultAzureCredential chain to AzureCliCredential by setting AZURE_TOKEN_CREDENTIALS to AzureCliCredential. The string comparison is performed in a case-insensitive manner.
🌐
GitHub
github.com › Azure › azure-sdk-for-python › blob › main › sdk › identity › azure-identity › README.md
azure-sdk-for-python/sdk/identity/azure-identity/README.md at main · Azure/azure-sdk-for-python · GitHub
As of version 1.14.0, DefaultAzureCredential attempts to authenticate with all developer credentials until one succeeds, regardless of any errors previous developer credentials experienced. For example, a developer credential may attempt to get a token and fail, so DefaultAzureCredential will continue to the next credential in the flow.
🌐
Windows
azuresdkdocs.z19.web.core.windows.net › python › azure-identity › 1.22.0 › _modules › azure › identity › _credentials › default.html
azure.identity._credentials.default — Azure SDK for Python 2.0.0 documentation
[docs] class DefaultAzureCredential(ChainedTokenCredential): """A credential capable of handling most Azure SDK authentication scenarios. For more information, See `Usage guidance for DefaultAzureCredential <"https://aka.ms/azsdk/python/identity/credential-chains#usage-guidance-for-default...
🌐
Jongallant
blog.jongallant.com › home › 2021 › azure identity 101 - defaultazurecredential
Jon Gallant - Azure Identity 101 - DefaultAzureCredential
August 11, 2021 - DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); SecretClient client = new SecretClientBuilder() .vaultUrl(vaultUri) .credential(defaultCredential) .buildClient(); } ```text · ```python · cred = DefaultAzureCredential() client = SecretClient(vault_url=vault_url, credential=cred) ```text ·