Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

send data to create incident to Dynamics 365 with Python Requests post() Method

I try to writing code to send data to create incident to Dynamics 365 with Python Requests post() Method but I have issue. I use python 3.6, I use this code but it give me this error

{'error': {'code': '', 'message': "The requested resource does not support http method 'POST'."}}

the code:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me suggest few things to fix and test the code.

  1. Entity name should be corrected

Both of the below endpoints should work, but incidents is the right entity name.

crmwebapi = 'https://xxxxxx.api.crm4.dynamics.com/api/data/v9.1/incidents'
crmwebapi = 'https://xxxxxx.crm4.dynamics.com/api/data/v9.1/incidents' 
  1. Remove Prefer headers, as they are only needed for GET requests

Try the below headers.

crmrequestheaders = {
        'Authorization': 'Bearer ' + accesstoken,
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0',
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
    }
  1. While assigning lookup/navigation properties - use @odata.bind

Check below and adjust accordingly.

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)",
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...