Skip to content

Instantly share code, notes, and snippets.

Last active August 17, 2018 15:29
What would you like to do?
Calling an Azure Active Directory (AAD) secured Azure Function from Python
import json
import requests
with open('settings.json', 'r') as fp:
settings = json.load(fp)
CLIENT_ID = settings['clientId']
CLIENT_SECRET = settings['clientSecret']
TENANT_ID = settings['tenantId']
APP_ID_URI = settings['appIdUri']
auth_body = {
'grant_type': 'client_credentials',
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'resource': APP_ID_URI
auth_uri = '{}/oauth2/token'.format(TENANT_ID)
auth_resp =, data=auth_body, headers={'Content-Type': 'application/x-www-form-urlencoded'})
if not auth_resp.ok:
print('Failed to authenticate')
bearer_token = auth_resp.json()['access_token']
res = requests.get(
'https://<functions app><function>?code=<function key>',
headers = {'Authorization': 'Bearer {}'.format(bearer_token)})
print(json.dumps(res.json(), indent=2))
"tenantId": "<Azure tenant ID>,
"clientId": "<Application ID>",
"clientSecret": "<Application Client Secret>",
"appIdUri": "<Application ID URI>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment