1

我需要使用 Python API 从 Pardot Saleforce 对象中获取数据。

有人可以分享任何可用的片段以使用 Python 从所有 Pardot 对象(表)中获取数据。

4

1 回答 1

0

我正在使用 pypardot4 开发 Pardot 同步解决方案(感谢 Matt 的https://github.com/mneedham91/PyPardot4),它涉及通过 API (v4) 检索数据。以下是访问者 API 的一些片段,但您几乎可以对任何 Pardot API 使用相同的片段(访问...除外):

from pypardot.client import PardotAPI

# ... some code here to read API config ...
email = config['pardot_email']
password = config['pardot_password']
user_key = config['pardot_user_key']

client = PardotAPI(email, password, user_key)
client.authenticate()

# plain query
data = client.visitors.query(sort_by='id')
total = data['total_results']
# beware - max 200 results are returned, need to implement pagination using offset query paramerter

# filtered query
data = client.visitors.query(sort_by='id', id_greater_than=last_id)

此外,我还使用了一些自省来遍历我设置的 API 配置数据,如下所示:

apiList = config['apiList']

# loop through the apis, and call their query method
for api in apiList:
    api_name = api['apiName']
    api_object_name = api['clientObject']
    api_object = getattr(client, api_object_name)
    method = 'query'

    if api.get('noSortBy', False) == False:
        data = getattr(api_object, method)(created_after=latest_sync, sort_by='created_at')
    else:
        # The API is not consistent, the sort_by criteria is not supported by all resources
        data = getattr(api_object, method)(created_after=latest_sync)

以及来自 apiList 配置 JSON 的片段:

    "apiList":[
        {
            "apiName": "campaign",
            "clientObject": "campaigns"
        },
        {
            "apiName": "customField",
            "clientObject": "customfields"
        },
        {
            "apiName": "customRedirect",
            "clientObject": "customredirects"
        },
        {
            "apiName": "emailClick",
            "clientObject": "emailclicks",
            "noSortBy": true
        },
...

注意 noSortBy 字段以及它在代码中的处理方式。

希望这可以帮助!

于 2019-06-07T13:32:24.860 回答