Python Dynamics 365包读写数据

问题描述 投票:0回答:1

我们设法用token auth连接到我们的dynamics 365云实例。然而,我们希望能找到类似"冻力".

该包"。https:/libraries.iopypidynamics365crm-python。"似乎比较新,但似乎只能处理标准对象,而不能处理自定义对象。

我们目前的解决方案只适用于REST。

import requests
import json

#set these values to retrieve the oauth token
crmorg = 'https://org.crm4.dynamics.com' #base url for crm org
clientid = '<id>' #application client id
client_secret = '<secret>'
username = '[email protected]' #username
userpassword = 'pw' #password
tokenendpoint = 'https://login.microsoftonline.com/bb23defa-be1d-4137-969b-324f8468f15a/oauth2/token' #oauth token endpoint
authorizationendpoint = 'https://login.microsoftonline.com/bb23defa-be1d-4137-969b-324f8468f15a/oauth2/authorize'

#build the authorization token request
tokenpost = {
    'client_id':clientid,
    'resource':crmorg,
    'client_secret':client_secret,
    'username':username,
    'password':userpassword,
    'grant_type':'password',
    'oauthUrl':authorizationendpoint
}

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

#set these values to query your crm data
crmwebapi = 'https://<org>.crm4.dynamics.com/api/data/v9.1' #full path to web api endpoint
crmwebapiquery = '/new_households' #web api query (include leading /)


#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()['value'][0]

    #loop through it
    for key,value in crmresults.items():
        print (key, value)

except KeyError:
    #handle any missing key errors
    print('Could not parse CRM results')

有谁知道一个包?

python dynamics-crm microsoft-dynamics
1个回答
0
投票

REST API是最好的方式。

我们每次使用它们与JavaScript和parase响应为Json。

下面是我在crm中执行的示例调用,你会看到响应被转化为Json。

不要太在意细节,对你来说重要的是这句话 var results = JSON.parse(this.response);

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?$select=accountcategorycode,accountclassificationcode,accountid,accountnumber,accountratingcode", false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=10");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var accountcategorycode = results.value[i]["accountcategorycode"];
                var accountcategorycode_formatted = results.value[i]["[email protected]"];
                var accountclassificationcode = results.value[i]["accountclassificationcode"];
                var accountclassificationcode_formatted = results.value[i]["accountclassificationcode@OData.Community.Display.V1.FormattedValue"];
                var accountid = results.value[i]["accountid"];
                var accountnumber = results.value[i]["accountnumber"];
                var accountratingcode = results.value[i]["accountratingcode"];
                var accountratingcode_formatted = results.value[i]["[email protected]"];
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
© www.soinside.com 2019 - 2024. All rights reserved.