InterSystems IRIS 快速连接 Salesforce REST API 实战指南

作者:API传播员 · 2025-10-14 · 阅读时间:4分钟
本文详细介绍了如何从InterSystems IRIS环境通过嵌入式Python连接到Salesforce REST API,包括配置Connected App、安装必要的Python包requests以及编写示例代码访问Salesforce数据。REST API提供了一种高效的方式,帮助开发者轻松集成Salesforce数据到自己的应用程序中。

一. Salesforce REST API概述

Salesforce 是全球知名的企业软件公司,其Salesforce CRM 广泛应用于客户关系管理。通过 Salesforce,企业可以通过电话、电子邮件、社区、媒体和社交平台管理客户互动,涵盖销售、支持和营销等核心流程。

更重要的是,Salesforce 完全基于云端,便于随时随地访问数据和服务。本文将重点介绍如何从 InterSystems IRIS 或其他 InterSystems 产品连接到 Salesforce,并通过其 REST API 访问数据。


二. 配置 Salesforce REST API

在开始访问 Salesforce 数据之前,需要完成基础配置。

1. 配置 Connected App

  1. 创建 Connected App
    登录 Salesforce 控制台,导航至 应用程序 -> 应用程序管理器,点击 新建 Connected App

  2. 填写必要参数
    系统会生成 Consumer KeyConsumer Secret,请妥善保存。

  3. 编辑策略参数
    进入 安装程序 -> 平台工具 -> 应用程序 -> 连接的应用程序 -> 管理连接的应用程序,找到刚创建的 Connected App,编辑其策略:

    • 允许用户:所有用户可自我授权


完成以上步骤后,您即可通过 REST API 安全访问 Salesforce 数据。


三. 使用 InterSystems IRIS 连接 Salesforce

完成 Salesforce 配置后,可通过 InterSystems IRIS 的嵌入式 Python 访问 Salesforce。

1. 安装必要的 Python 包

在 InterSystems IRIS 容器中安装 requests 包,用于发送 HTTP 请求:

pip3 install --target /usr/irissys/mgr/python requests

2. 编写示例代码

以下示例演示如何通过 Salesforce REST API 获取数据。请确保已获取访问令牌:

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'

        # OAuth2密码模式认证请求
        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())

3. 关键参数说明

  • grant_type:示例使用 password 模式
  • client_id / client_secret:对应 Salesforce Connected App 中生成的 Consumer KeyConsumer Secret
  • username / password:Salesforce 账户凭证

通过上述步骤,您即可在 InterSystems IRIS 中成功访问 Salesforce REST API 并获取对象数据。


四. 总结

本文介绍了如何配置 Salesforce REST API,并通过 InterSystems IRIS 使用嵌入式 Python 连接 Salesforce。REST API 提供了一种简便高效的方式,让开发者能够轻松将 Salesforce 数据集成到自己的应用中。

通过本文的实践示例,您可以快速实现数据访问与集成,为企业或项目提供可靠的 Salesforce 数据支持。

原文链接: https://es.community.intersystems.com/post/conectando-salesforce-rest-api