Python:使用批量API V3向mailchimp添加订阅者

时间:2017-03-16 07:48:40

标签: python mailchimp mailchimp-api-v3.0

我试图编写一个函数来在mailchimp中创建订阅者,但是我收到错误,我无法理解错误。

我正在使用python-mailchimp(是的,我有正确的list-id而不是000000)

我的代码:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/celery/local.py", line 188, in __call__
    return self._get_current_object()(*a, **kw)
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/celery/app/task.py", line 420, in __call__
    return self.run(*args, **kwargs)
  File "/home/USER/.virtualenvs/DOMAIN/PROJECT/apps/members/tasks.py", line 2340, in update_mailchimp
    batch = client.batches.create(data={"operations" : [{"method":"POST", "path":"/lists/000000/members/", "body":databody}]})
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/mailchimp3/entities/batches.py", line 53, in create
    return self._mc_client._post(url=self._build_path(), data=data)
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/mailchimp3/mailchimpclient.py", line 25, in wrapper
    return fn(self, *args, **kwargs)
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/mailchimp3/mailchimpclient.py", line 71, in _post
    r.raise_for_status()
  File "/home/USER/.virtualenvs/DOMAIN/lib/python3.4/site-packages/requests/models.py", line 862, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://us12.api.mailchimp.com/3.0/batches

这是追溯:

"body":json.dump(databody)

任何人都能看到我做错了什么?

更新

如果我Schema describes object, array found instead我没有收到相同的错误,Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim iColor As Integer '// From Ivan Moala '// http://www.mrexcel.com/hof001.php '// Amended routine found on this Web site '// Note: Don't use IF you have Conditional '// formating that you want to keep! '// On error resume in case '// user selects a range of cells On Error Resume Next iColor = Target.Interior.ColorIndex 'Leave On Error ON for Row offset errors If iColor < 0 Then iColor = 48 Else iColor = iColor + 1 End If '// Need this test incase Font color is the same If iColor = Target.Font.ColorIndex Then iColor = iColor + 1 Cells.FormatConditions.Delete '// Horizontal color banding With Range("A" & Target.Row, Target.Address) 'Rows(Target.Row) .FormatConditions.Add Type:=2, Formula1:="TRUE" .FormatConditions(1).Interior.ColorIndex = iColor End With '// Vertical color banding With Range(Target.Offset(1 - Target.Row, 0).Address & ":" & Target.Offset(-1, 0).Address) 'Rows(Target.Row) .FormatConditions.Add Type:=2, Formula1:="TRUE" .FormatConditions(1).Interior.ColorIndex = iColor End With End Sub

2 个答案:

答案 0 :(得分:4)

这是正确的方法

from mailchimp3 import MailChimp
def export_to_mailchimp():

    #Get all users in DB
    users = User.objects.all()


    #Create list for dictionaries
    operations = []


    #loop through queryset
    for userobject in users:

        #Create dictionary for body
        databody_item = {
            "email_address": item.email,
            "status": "subscribed",
            "merge_fields": {
                "FNAME": item.first_name,
                "LNAME": item.last_name
            }
        }
        #Create dictionary for a operation
        operation_item = {"method":"POST", "path":"/lists/000000/members/", "body":json.dumps(databody_item)}
        #Append to list
        operations.append(operation_item)    


    client = MailChimp(settings.MAILCHIMP_USER, settings.MAILCHIMP_KEY)
    batch = client.batches.create(data={"operations": operations})

答案 1 :(得分:0)

这是工作中的人:

client = MailChimp(USERNAME, APIKEY)
def fill_list(list_id, subscribers_data):
    data = {'operations': create_subscriptions_data(list_id, subscribers_data)}
    client.batches.create(data)

def create_subscriptions_data(list_id, users_data):
    return [{'method': 'PUT',
             'path': 'lists/{}/members/{}'.format(list_id, str(md5(user['email_address'].encode('utf-8')))),
             'body': json.dumps(user)} for user in users_data]

我猜想方法的问题需要发布。更详细的信息可以在下面的链接中找到。 https://devask.cz/questions/42175544/how-to-unsubscribe-delete-list-members-using-mailchimp3-in-python