I am trying to use managed identity of Azure function to access AAD protected web app, which requires a custom flow instead of using different clients. So the first step is to obtain an access token:
credential = DefaultAzureCredential()
scope = "https://graph.microsoft.com/.default"
token = credential.get_token(scope)
I am able to get a token successfully.
Then access the AAD protected web app:
uri = "https://my-web-app-1.azurewebsites.net/"
headers = {
'Authorization': 'Bearer ' + token.token
}
api_response = requests.get(uri, headers=headers)
However, this step returns error:
{"code":401,"message":"IDX10511: Signature validation failed. Keys tried: '[PII is hidden]'. \nkid: '[PII is hidden]'. \nExceptions caught:\n '[PII is hidden]'.\ntoken: '[PII is hidden]'."}
So I suspect I used a wrong scope to get the token. So I am confused which scope to use here?
------context-----
I have an azure function which has been enabled system identity which has been enabled to access my web app my-web-app-1.azurewebsites.net/
. The web app my-web-app-1.azurewebsites.net/
is AAD protected.
Here is the API permissions under my AAD application which I used for the web app authentication. The mistake here is I set API permissions for web app instead of Azure functions which in case has no way to set (because Functions have a system identity instead of AAD application where we can set API permissions.)