Salesforce Rest API 查找数据

官方文档:

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/using_resources_working_with_records.htm

如何生成access token? 其中client id 和 client secret可以在connected app里找到,username和password为Salesforce里一个user的用户名和 密码 + security token


import requests as req
import json
 
testTokenUrl = "https://test.salesforce.com/services/oauth2/token"
testReqJson = {
    "client_id": "xxx",
    "client_secret": "xxx",
    "username": "sandbox_dev",
    "password": "password+securityToken",
    "grant_type": "password"
}
 
header = {
    "Content-Type": "application/x-www-form-urlencoded",
}
 
response = req.post(testTokenUrl, data=testReqJson, headers=header)
if response.status_code > 200:
    print (response.text)
else:
    token = json.loads(response.text)
    accessToken = json.loads(response.text)['access_token']

比较通用的方法是通过sql语句查询所需的数据

def get_record_type_id(token):
    header = {
        "Authorization": "Bearer " + token['access_token']
    }
    queryURL = token['instance_url'] + "/services/data/v47.0/query/?q="
    queryParams = "Select+Id+FROM+RecordType+WHERE+Name='Root Branch'"
    response = req.get(queryURL+queryParams, headers=header)
    
    if response.status_code == 200:
        return json.loads(response.text)['records'][0]['Id']
    else:
        raise ImportException('Error: '+response.text)

发表评论

电子邮件地址不会被公开。 必填项已用*标注