Try to set scope as {your-api-client-id}/.default to get access token. Replace your-api-client-id with the client id/application id for your API app in Azure AD.

Generate longer token for Databricks with Azure.
python 3.x - How to properly use "get_token" of "ManagedIdentityCredential" of azure identity - Stack Overflow
DefaultAzureCredential doesn't authenticate successfully (even when logged in via CLI
databricks - Python DefaultAzureCredential get_token set expiration or renew token - Stack Overflow
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
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)
When I use the .net Microsoft.IdentityModel.Clients.ActiveDirectory library I don’t have to think about scope.
Actually, you have thought about it. When you use the code below via .net sdk, there is an aasUrl, this is the equivalent you need to specify in python sdk.
AuthenticationResult authenticationResult = await authContext.AcquireTokenAsync(aasUrl, clientCred);
With Microsoft.IdentityModel.Clients.ActiveDirectory, the method AcquireTokenAsync essentially uses the Azure AD client credential flow v1.0 endpoint to get the token, so this parameter is named as resource, i.e. the resource parameter in this method AcquireTokenAsync(String, ClientCredential).
In python sdk azure.identity, this method get_token essentially uses Azure AD client credential flow v2.0 endpoint to get the token, when v1.0 endpoint migrates to v2.0, there are some changes, one of them is the resource, it changed to scope, see this doc. When using scope, you need to specify the permission you want to access, you can also use /.default, then by default all the permissions added to the app will be requested.
So in your case, you just need to use the scope as aasUrl/.default, e.g. https://management.azure.com/.default, it depends on yourself.
token = credential.get_token("https://management.azure.com/.default")
Also have a test with the code on my side, it works fine.

if you are using Graph API you could use as
https://graph.microsoft.com/.default
https://graph.microsoft.com/Directory.Read
https://graph.microsoft.com/user_impersonation
if you are using Azure Resource Manager API then the base uri would be https://management.core.windows.net followed by the necessary permission.
If you want to acquire a token for all the static scopes of a v1.0 application, append ".default" to the app ID URI of the API
You Will see the APP Id uri in the Application Overview page

if you have not created app id uri, you set the uri and add permission scope