Hi, I came across the same error while working with the OAuth v2.0 on-behalf-of flow using the Python library azure-identity (1.12.0) and following the OAuth v2.0 on-behalf-of flow documentation. The same error AADSTS50013
was thrown when I tried to use the get_token
method of the OnBehalfOfCredential class.
I solved it by having the correct aud
claim in the user's access token when instantiating a OnBehalfOfCredential
object. This was mentioned in the documentation:
"This token must have an audience (aud
) claim of the app making this OBO request (the app denoted by the client-id
field)."
To do so, in the Expose an API
tab of the app registration I added an application ID URI: api://{client_id}, a scope called user_impersonation
and authorised a client application to have the scope user_impersonation
so that no consent is required: 04b07795-8ddb-461a-bbee-02f9e1bf7b46 (Azure CLI). I looked up the ID in the documentation.
If I requested a token as a user with the Azure CLI:
az account get-access-token --tenant {tenant_id} --scope api://{client_id}/user_impersonation
...the aud
claim was api://{client_id}/user_impersonation
I then added the delegated permission in the API permissions
tab of the app registration: user_impersonation which allowed me to get a token with the scope {client_id}/user_impersonation
or {client_id}/.default
:
az account get-access-token --tenant {tenant_id} --scope {client_id}/user_impersonation
The aud
claim became {client_id}
which solved the problem for me when using the OnBehalfOfCredential
class. The user's access token was used as the user_assertion
parameter.