使用 API 调用 Campaign Monitor with Create Send & Python 获取订阅者

Getting Subscribers using an API call to Campaign Monitor with Create Send & Python

阿罗哈朋友,

对于这个菜鸟问题深表歉意,我在 Python 和编程方面还处于起步阶段,我正试图找到一种更好的方法来完成一项非常非常手动的任务。

我想使用它 API

从 Campaign Monitor 获取所有订阅者

我反复阅读了 API 文档,得出的结论是我需要使用这段代码:

 from __future__ import absolute_import

import json

from createsend.createsend import CreateSendBase, BadRequest
from createsend.utils import json_to_py, validate_consent_to_track


class Subscriber(CreateSendBase):
    """Represents a subscriber and associated functionality."""

    def __init__(self, auth=None, list_id=None, email_address=None):
        self.list_id = list_id
        self.email_address = email_address
        super(Subscriber, self).__init__(auth)

    def get(self, list_id=None, email_address=None, include_tracking_preference=False):
        """Gets a subscriber by list ID and email address."""
        params = {
            "email": email_address or self.email_address,
            "includetrackingpreference": include_tracking_preference,
        }
        response = self._get("/subscribers/%s.json" %
                             (list_id or self.list_id), params=params)
        return json_to_py(response)

我对代码有点迷茫,我不确定是否需要使用 API 键在上面附加创建发送 class,上面的代码会给我吗我的订阅者的完整 Json 列表..?

我目前正在 Udemy 上学习基础 API 课程,所以我知道如何使用邮递员和使用 Flask 运行 基本 api 调用,但我还没有见过或用过Createsend。

Github 代码 here

文档here

对于阅读本文的任何人,我非​​常感谢您抽出宝贵的时间(无论您是否回复)!

您真诚的, 数据新手。

来自另一位新手的问候。昨天我在尝试使用他们的 API 向列表添加订阅者时遇到了您的 post。纠结了好久,只能在资深开发者的帮助下解决了。所以不要自责。我知道这可能有点晚了,但希望它仍然有帮助。如果你同时解决了它,请告诉我:-)

首先,如果你想让所有订阅者都加入一个列表,你需要查看这部分 https://www.campaignmonitor.com/api/lists/ 以上内容可能会一次让你获得一个订阅者。我建议使用像 Postman 这样的东西对 API 进行一些测试 GET 调用,看看哪个能得到你需要的结果。例如,这是我向列表的所有活动订阅者发出 GET 调用时得到的结果:

{
    "Results": [
        {
            "EmailAddress": "marco@polo.bro",
            "Name": "Marco",
            "Date": "2018-11-13 10:36:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        },
        {
            "EmailAddress": "marco@polo.broke",
            "Name": "Marco",
            "Date": "2018-11-13 10:38:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        },
        {
            "EmailAddress": "marco@polo.mkd",
            "Name": "Marco",
            "Date": "2018-11-13 17:22:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        },
        {
            "EmailAddress": "marco@polo.ro",
            "Name": "Marco",
            "Date": "2018-11-13 09:52:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        },
        {
            "EmailAddress": "subscriber1@example.com",
            "Name": "New Subscriber",
            "Date": "2018-11-13 16:55:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        },
        {
            "EmailAddress": "subscriber2@example.com",
            "Name": "New Subscriber 2",
            "Date": "2018-11-13 16:59:00",
            "State": "Active",
            "CustomFields": [],
            "ReadsEmailWith": ""
        }
    ],
    "ResultsOrderedBy": "email",
    "OrderDirection": "asc",
    "PageNumber": 1,
    "PageSize": 1000,
    "RecordsOnThisPage": 6,
    "TotalNumberOfRecords": 6,
    "NumberOfPages": 1
}

否则,试试这个(你应该创建订阅者的实例 class):

from createsend import * 

list_id = 'your-list-id'
subscriber = Subscriber(
    {'api_key': 'your-api-key'},
    list_id,
    email_address=None
)

user_email = 'user@example.com'
subscriber.get(list_id, user_email, include_tracking_preference=True|False)

我认为您也可以直接 subscriber.get() 获取详细信息而无需任何参数。

希望对您有所帮助。

编码愉快 :-)