在開始之前,需要先完成 Salesforce REST API 的基礎配置。以下是主要步驟:
創(chuàng)建 Connected App
登錄 Salesforce 控制臺,導航到 應用程序 -> 應用程序管理器,然后點擊 新建 Connected App。

填寫必要參數(shù)
配置完成后,系統(tǒng)會生成 Consumer Key 和 Consumer Secret,請妥善保存這些信息。

編輯策略參數(shù)
進入 安裝程序 -> 平臺工具 -> 應用程序 -> 連接的應用程序 -> 管理連接的應用程序,找到剛創(chuàng)建的 Connected App,編輯其策略參數(shù):

完成以上步驟后,您即可通過 REST API 訪問 Salesforce 數(shù)據(jù)。
在完成 Salesforce 的基礎配置后,接下來我們將通過 InterSystems IRIS 使用嵌入式 Python 連接到 Salesforce。
在 InterSystems IRIS 容器中安裝 requests 包,用于發(fā)送 HTTP 請求:
pip3 install --target /usr/irissys/mgr/python requests
以下是一個示例類,用于通過 REST API 訪問 Salesforce 數(shù)據(jù)。在運行代碼之前,請確保已獲取訪問令牌。
import requests
class SalesforceConnTest:
@staticmethod
def conn_test():
consumer_key = 'xxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxx'
domain_name = 'https://intersystems4-dev-ed.develop.my.salesforce.com'
username = 'mymail@domain.com'
password = 'mypassword'# 身份驗證請求數(shù)據(jù)
json_data = {
'grant_type': 'password',
'client_id': consumer_key,
'client_secret': consumer_secret,
'username': username,
'password': password
}# 獲取訪問令牌
response_access_token = requests.post(
f"{domain_name}/services/oauth2/token",
data=json_data
)
print(response_access_token.status_code)
print(response_access_token.reason)
print(response_access_token.json()) if response_access_token.status_code == 200:
access_token_id = response_access_token.json()["access_token"]
print('Access token created')# 使用訪問令牌請求 Salesforce 對象
headers = {"authorization": f"Bearer {access_token_id}"}
response_sobject = requests.get(
f"{domain_name}/services/data/v53.0/sobjects",
headers=headers
)
print(response_sobject.reason)
print(response_sobject.json())
password 模式。Consumer Key 和 Consumer Secret。通過上述代碼,您可以成功連接到 Salesforce,并訪問其提供的 REST API 數(shù)據(jù)。
通過本文的介紹,我們學習了如何配置 Salesforce REST API,并通過 InterSystems IRIS 使用嵌入式 Python 連接到 Salesforce。REST API 提供了一種高效的方式,幫助開發(fā)者輕松集成 Salesforce 數(shù)據(jù)到自己的應用程序中。希望本文的內(nèi)容能為您在實際項目中帶來幫助。
原文鏈接: https://es.community.intersystems.com/post/conectando-salesforce-rest-api