I have managed to get a token from oauth2 in my python app. However when I try and get some results I get a 401. Below is my code.
crmapi = "https://{orgname}.api.crm4.dynamics.com/api/data/v9.1/"
logging.info('Python HTTP trigger function processed a request.')
crmorg = 'https://{orgname}.crm4.dynamics.com/'
clientid = '0e91010b-...….'
username = username
userpassword = 'password'
tokenendpoint = 'login.microsoftonline.com/.../token
crmwebapi = 'https://{orgname}.api.crm4.dynamics.com/api/data/v9.1'
crmwebapiquery = '/opportunities(8A49E258-...….)'
tokenpost = {
'client_id':clientid,
'username':username,
'password':userpassword,
'grant_type':'password',
'client_secret': '........',
'scope':'https://{orgname}.crm.dynamics.com//user_impersonation'
}
tokenres = requests.post(tokenendpoint, data=tokenpost)
accesstoken = ''
try:
accesstoken = tokenres.json()['access_token']
except(KeyError):
#handle any missing key errors
print('Could not get access token')
if(accesstoken!=''):
#prepare the crm request headers
crmrequestheaders = {
'Authorization': 'Bearer ' + accesstoken,
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Prefer': 'odata.maxpagesize=500',
'Prefer': 'odata.include-annotations=OData.Community.Display.V1.FormattedValue'
}
crmres = requests.get(crmwebapi+crmwebapiquery, headers=crmrequestheaders)