Python 和 Dynamics CRM:使用网络 api

Python and Dynamics CRM: using a web api

我想使用 Python 从我们的 CRM 数据开始创建电子邮件提醒。我知道 Dynamics365 有一些警报功能,但我想做更多。理想情况下,我想使用 CRM api。我正在尝试 运行 以下代码,并收到一条错误消息。

这对我来说是第一次开始。我没有 Azure,我更愿意找到一个解决方案,让我仅使用 API 和登录信息提取 CRM 数据。

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')

'''

看起来连接成功了 - 但没有为 crm.results 返回 "value"??

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 '''

如果您想使用 Dynamics Crm webapi,您需要在 azure AD 中注册应用程序并分配适当的权限。然后你会得到Appid,你可以调用API并执行操作。 有关于这一切的好视频。 这里是linkhttps://youtu.be/Td7Bk3IXJ9s