Dynamics 365 CRM not able to access via API python code.

Aman Dhaka 1 Reputation point
2021-06-08T13:24:06.537+00:00

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')
Community Center | Not monitored
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-06-08T13:31:01.877+00:00

    Dynamics CRM is not currently supported here on QnA. The product group for Dynamics actively monitors questions over at
    https://community.dynamics.com/crm

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.