使用 Python 请求 post() 方法发送数据以创建事件到 Dynamics 365
send data to create incident to Dynamics 365 with Python Requests post() Method
我尝试编写代码以使用 Python 请求 post() 方法发送数据以创建事件到 Dynamics 365,但我遇到了问题。
我使用 python 3.6,
我使用这段代码,但它给了我这个错误
{'error': {'code': '', 'message': "The requested resource does not support http method 'POST'."}}
代码:
import requests
import json
#set these values to retrieve the oauth token
crmorg = 'https://xxxxxx.crm4.dynamics.com/' #base url for crm org
clientid = 'xxxxxxxxxxx' #application client id
tokenendpoint = 'https://login.microsoftonline.com/xxxxxxxxxx/oauth2/token' #oauth token endpoint
secret = 'xxxxxxxxxxxxxxxxx'
#set these values to query your crm data
crmwebapi = 'https://xxxxxx.api.crm4.dynamics.com/api/data/v9.1/Incident' #full path to web api endpoint
#crmwebapiquery = 'contacts?$select=fullname,contactid' #web api query (include leading /)
#build the authorization token request
tokenpost = {
'client_id':clientid,
'resource':crmorg,
'client_secret' : secret,
'grant_type':'client_credentials',
}
#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'
}
incidentdata={
"title": "test",
"customerid_account": "xxxxxxxxxxxxxxx",
"forte_casetypeid": "xxxxxxxxxxxxx",
"forte_casecategoryid": "xxxxxxxxxxxxxxxxxxx",
"primarycontactid": "xxxxxxxxxxxxxxxxx",
"entitlementid": "xxxxxxxxxxxxxxxxxxxxx",
}
#make the crm request
crmres = requests.post(crmwebapi, headers=crmrequestheaders, data=json.dumps(incidentdata))
try:
#get the response json
crmresults = crmres.json()
print(crmresults)
except KeyError:
#handle any missing key errors
print('Could not parse CRM results')
让我提出一些建议来修复和测试代码。
- 应更正实体名称
以下两个端点都应该有效,但 incidents
是正确的实体名称。
crmwebapi = 'https://xxxxxx.api.crm4.dynamics.com/api/data/v9.1/incidents'
crmwebapi = 'https://xxxxxx.crm4.dynamics.com/api/data/v9.1/incidents'
- 删除
Prefer
headers,因为只有 GET
个请求需要它们
试试下面的 headers。
crmrequestheaders = {
'Authorization': 'Bearer ' + accesstoken,
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
}
- 分配 lookup/navigation 属性时 - 使用
@odata.bind
检查下方并相应调整。
incidentdata={
"title": "test",
"customerid_account@odata.bind": "/accounts(xxxxxxxxxxxxxxx)",
"forte_casetypeid": "xxxxxxxxxxxxx",
"forte_casecategoryid": "xxxxxxxxxxxxxxxxxxx",
"primarycontactid@odata.bind": "/contacts(xxxxxxxxxxxxxxxxx)",
"entitlementid@odata.bind": "/entitlements(xxxxxxxxxxxxxxxxxxxxx)",
}
我尝试编写代码以使用 Python 请求 post() 方法发送数据以创建事件到 Dynamics 365,但我遇到了问题。 我使用 python 3.6, 我使用这段代码,但它给了我这个错误
{'error': {'code': '', 'message': "The requested resource does not support http method 'POST'."}}
代码:
import requests
import json
#set these values to retrieve the oauth token
crmorg = 'https://xxxxxx.crm4.dynamics.com/' #base url for crm org
clientid = 'xxxxxxxxxxx' #application client id
tokenendpoint = 'https://login.microsoftonline.com/xxxxxxxxxx/oauth2/token' #oauth token endpoint
secret = 'xxxxxxxxxxxxxxxxx'
#set these values to query your crm data
crmwebapi = 'https://xxxxxx.api.crm4.dynamics.com/api/data/v9.1/Incident' #full path to web api endpoint
#crmwebapiquery = 'contacts?$select=fullname,contactid' #web api query (include leading /)
#build the authorization token request
tokenpost = {
'client_id':clientid,
'resource':crmorg,
'client_secret' : secret,
'grant_type':'client_credentials',
}
#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'
}
incidentdata={
"title": "test",
"customerid_account": "xxxxxxxxxxxxxxx",
"forte_casetypeid": "xxxxxxxxxxxxx",
"forte_casecategoryid": "xxxxxxxxxxxxxxxxxxx",
"primarycontactid": "xxxxxxxxxxxxxxxxx",
"entitlementid": "xxxxxxxxxxxxxxxxxxxxx",
}
#make the crm request
crmres = requests.post(crmwebapi, headers=crmrequestheaders, data=json.dumps(incidentdata))
try:
#get the response json
crmresults = crmres.json()
print(crmresults)
except KeyError:
#handle any missing key errors
print('Could not parse CRM results')
让我提出一些建议来修复和测试代码。
- 应更正实体名称
以下两个端点都应该有效,但 incidents
是正确的实体名称。
crmwebapi = 'https://xxxxxx.api.crm4.dynamics.com/api/data/v9.1/incidents'
crmwebapi = 'https://xxxxxx.crm4.dynamics.com/api/data/v9.1/incidents'
- 删除
Prefer
headers,因为只有GET
个请求需要它们
试试下面的 headers。
crmrequestheaders = {
'Authorization': 'Bearer ' + accesstoken,
'OData-MaxVersion': '4.0',
'OData-Version': '4.0',
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
}
- 分配 lookup/navigation 属性时 - 使用
@odata.bind
检查下方并相应调整。
incidentdata={
"title": "test",
"customerid_account@odata.bind": "/accounts(xxxxxxxxxxxxxxx)",
"forte_casetypeid": "xxxxxxxxxxxxx",
"forte_casecategoryid": "xxxxxxxxxxxxxxxxxxx",
"primarycontactid@odata.bind": "/contacts(xxxxxxxxxxxxxxxxx)",
"entitlementid@odata.bind": "/entitlements(xxxxxxxxxxxxxxxxxxxxx)",
}