I have created the azure function and the app is successfully registered, add authentication to the function app. Now if hit the URL using postman I have the option of passing a token before hitting the URL. But want I want to do is automate the authentication part from code that gets token and verifies it. Sharing my code below.
def authenticate_client_key():
authority_host_uri = 'https://login.microsoftonline.com'
tenant = '<tenantid>'
authority_uri = authority_host_uri + '/' + tenant
resource_uri = 'https://management.core.windows.net/'
client_id = '<clientid'
client_secret = '<clientsecret>'
context = adal.AuthenticationContext(authority_uri, api_version=None)
mgmt_token = context.acquire_token_with_client_credentials(resource_uri, client_id, client_secret)
credentials = AADTokenCredentials(mgmt_token, client_id)
return credentials
here credentials is of type dict, from which I create a header - {"Authorization":"Bearer {}".format(bearer_token)}.
this header needs to added to the req object in the main method of function app.
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
inside this req object
- I tried adding req.update(headers=credentials) but failed