Hi ALl,
I am trying to fetch data via python code but getting below error not sure , what is missing ,
1) I did AD registration of Application as per the , https://sank8sinha.wordpress.com/register-application-in-azure-active-directory/
2) Below is my code i am trying to call api,
import requests
import json
crmorg = 'https://.crm4.dynamics.com/' # base url for crm org
clientid = '' # application client id
username = '@0af1.com' # username
userpassword = '' # password
tokenendpoint = 'https://login.microsoftonline.com//oauth2/v2.0/token' # oauth token endpoint
set these values to query your crm data
https://daznstaging.api.crm4.dynamics.com/api/data/v9.2/
crmwebapi = 'https://***api.crm4.dynamics.com/api/data/v9.2/' # full path to web api endpoint
crmwebapiquery = '/contacts?$select=fullname,contactid' # web api query (include leading /)
tokenpost = {
'client_id': clientid,
'resource': crmorg,
'username': username,
'password': userpassword,
'grant_type': 'password'
}
make the token request
tokenres = requests.post(tokenendpoint, data=tokenpost)
set accesstoken variable to empty string
accesstoken = ''
extract the access token
try:
accesstoken = tokenres.json()['access_token']
except(KeyError):
# handle any missing key errors
print('Could not get access token')
if we have an accesstoken
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'
}
# make the crm request
crmres = requests.get(crmwebapi + crmwebapiquery, headers=crmrequestheaders)
try:
# get the response json
crmresults = crmres.json()
print(crmresults)
except KeyError:
# handle any missing key errors
print('Could not parse CRM results')