通过Create Send& amp;获取订阅者使用对Campaign Monitor的API调用蟒蛇

时间:2018-05-23 09:48:46

标签: python createsend

Aloha Friends,

对于那个愚蠢的问题道歉,我还处于初期阶段,并且还处于Python&编程和我试图找到一个更好的方法来做一个非常非常手动的任务。

我想让Campaign Monitor的所有订阅者使用它的API

我已阅读并重新阅读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​​密钥来支持上面的create send类,并且上面只是给我一个完整的Json我的订阅者列表..?

我现在正在Udemy上学习基础API课程,所以我知道如何使用邮差并使用Flask运行基本api调用,但我还没有看到或使用过Createsend。

Github代码here

文档here

对于阅读此内容的任何人,我非​​常感谢您的时间,(无论您是否回复)!

您诚挚的, Datanovice。

1 个答案:

答案 0 :(得分:1)

来自另一个新手的问​​候。昨天我尝试使用他们的API将订阅者添加到列表中时,出现在您的帖子中。我有点挣扎,只能在高级开发人员的帮助下解决。所以不要打自己我知道这可能会晚一点,但希望它能对您有所帮助。如果您同时解决了此问题,请告诉我:-)

首先,如果要使所有订阅者都进入列表,则需要查看此部分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
}

否则,请尝试以下操作(您应该创建Subscriber类的实例):

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()以获取没有任何参数的详细信息。

希望有帮助。

快乐编码:-)