1
votes

I'd like to start creating email alerts from our CRM data with Python. I know that Dynamics365 has some alerting capabilities, but I'd like to do more. Ideally I would like to use the CRM api. I'm trying to run the following code, and getting an error message.

This is the first start for me on this. I don't have Azure, I'd prefer to find a solution that lets me pull CRM data with the API and login info only.

import requests
from requests_ntlm import HttpNtlmAuth
import json

username = 'username'
userpassword = 'password'

#set these values to query your crm data
crmwebapi = 'API URL'
crmwebapiquery = '/contacts?$select=fullname,contactid'

crmrequestheaders = {
    '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'
}

print('making crm request . . .')
crmres = requests.get(crmwebapi+crmwebapiquery, 
headers=crmrequestheaders,auth=HttpNtlmAuth(username,userpassword))
print('crm response received . . .')
try:
    print('parsing crm response . . .')
    crmresults = crmres.json()
    for x in crmresults['value']:
        print (x['fullname'] + ' - ' + x['contactid'])
except KeyError:
    print('Could not parse CRM results')

'''

It looks like it's connecting sucesfully - but not returning a "value" for crm.results??

runfile('F:/CRM/python CRM query.py', wdir='F:/CRM') making crm request . . . crm response received . . . parsing crm response . . . Traceback (most recent call last):

File "", line 1, in runfile('F:/CRM/python CRM query.py', wdir='F:/CRM')

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "F:/CRM/python CRM query.py", line 26, in crmresults = crmres.json()

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\site-packages\requests\models.py", line 897, in json return complexjson.loads(self.text, **kwargs)

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\json__init__.py", line 348, in loads return _default_decoder.decode(s)

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\Users\Gittel\AppData\Local\Continuum\anaconda3\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value '''

1

1 Answers

2
votes

If you wish to use Dynamics Crm webapi, you need to register app in azure AD and assign proper permission. You will then get Appid using that you can call API and perform operation. There is good video about all this. Here is the link https://youtu.be/Td7Bk3IXJ9s