Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Security and hardening · Decision guide

Azure SDK for Python: no credential in DefaultAzureCredential's chain returns a token

Short answer

DefaultAzureCredential failed to retrieve a token from the included credentials means every credential in the chain was unavailable: no complete AZURE_CLIENT_ID/TENANT_ID/SECRET set, no managed identity reachable, and no az login session. With azure-identity 1.27, turn on azure.identity DEBUG logging, read each get_token failure line, then fix that one source: az login locally, a managed identity in Azure.

  • Applies to azure-identity 1.27.0 (Python 3.10+), Azure CLI 2.93, Azure VMs, App Service and local development
  • Updated
  • By Maaz Zindani, Senior Software Engineer

DefaultAzureCredential is not one credential. It is a list of them, tried one after another, and each one that cannot work in the current environment raises a CredentialUnavailableError and passes the turn on. The message you see, DefaultAzureCredential failed to retrieve a token from the included credentials, only appears when every credential on the list has said no. Azure's own troubleshooting guide says the same: all of them failed, each raising its own error.

So the summary line tells you nothing about the cause. The per-credential lines in the debug log do, and which of them matters depends on where the code runs: a laptop, an Azure host with a managed identity, or a server or container outside Azure.

Find your case

  1. If the code runs on your own machineThe developer-tool fallback has no session, usually no az login.
  2. If it runs on an Azure VM or App ServiceManaged identity is not enabled, not reachable, or the wrong client ID is requested.
  3. If it runs in a container, CI job or server outside AzureService principal variables are missing from the process environment.
  4. If a token is returned but the Azure service then answers 401 or 403A different account won the chain than the one you meant.

On a laptop with no Azure CLI session

On a developer machine the chain ends at developer tools such as the Azure CLI. The troubleshooting guide lists Please run 'az login' to set up account for the case where no account is logged in or the login has expired. A second trap is a process that cannot find az at all, for example an IDE or service started with a short PATH.

Is the CLI there, is anyone signed in, sign in as the account that has access

which az
az account show
az login

Restart the app from a shell where az is on the PATH; a running process keeps the environment it started with.

On an Azure VM or App Service

Deployed code normally relies on ManagedIdentityCredential. On a VM it asks the instance metadata service at 169.254.169.254. The guide gives three common reasons for failure: no identity enabled on the resource, a user-assigned client_id the resource does not carry (the message is The requested identity hasn't been assigned to this resource), or a proxy or gateway on the VM, which the metadata service does not support.

Test the endpoint directly. The reply contains a live token, so do not share it.

From the VM, the check Azure's guide uses

curl 'http://169.254.169.254/metadata/identity/oauth2/token?resource=https://management.core.windows.net&api-version=2018-02-01' -H "Metadata: true"
  • A JSON reply with an access_token field means the identity works and the problem is in how your code constructs the credential.
  • For a user-assigned identity, pass its client ID, as in DefaultAzureCredential(managed_identity_client_id="<client-id>").
  • On App Service the host supplies IDENTITY_ENDPOINT and IDENTITY_HEADER; if they are absent over SSH, no identity is enabled on the app.

In a container or CI job outside Azure

Away from Azure there is no managed identity and no CLI session, so EnvironmentCredential is the only thing that can work. It needs a complete set before the application starts: AZURE_CLIENT_ID, AZURE_TENANT_ID and AZURE_CLIENT_SECRET, or AZURE_CLIENT_CERTIFICATE_PATH in place of the secret. The guide stresses that they must be set prior to application startup, so setting them from code after the credential exists does nothing.

Prints names only, not secret values

env | grep -oE '^AZURE_(CLIENT_ID|TENANT_ID|CLIENT_SECRET|CLIENT_CERTIFICATE_PATH)='
  • Three names printed means the set is complete.
  • Put the values in the job's secret settings, the container environment or the service unit, not in the image.

A token arrives but the service says 401 or 403

That is a different failure. Authentication worked, and the service refused the account that won. The guide notes this often means DefaultAzureCredential authenticated an account other than the intended one: the debug log names the credential that returned the token. Sign out of the developer tool that is winning, or exclude it with an exclude_..._credential argument, then give the right account its role assignment.

Pin the credential once the environment is known

Microsoft Learn describes setting AZURE_TOKEN_CREDENTIALS to a credential name, which shrinks the chain to that one credential. Individual names are supported from azure-identity 1.24.0, and category values prod and dev from 1.23.0. Pair it with DefaultAzureCredential(require_envvar=True) so a missing or misspelled value fails at startup. A failure then names the single credential you expect, instead of the whole list.

In the deployed environment only

export AZURE_TOKEN_CREDENTIALS=ManagedIdentityCredential

In short

  • The summary error means every credential on the list was unavailable; the debug log says why for each.
  • Laptop: az login. Azure host: enable the identity and check the metadata endpoint. Elsewhere: set the service principal variables before the process starts.
  • A 401 or 403 after a token is a role or wrong-account problem, not this error.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Explained for app owners

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.