Python:使用批量 API V3 向 mailchimp 添加订阅者
Python: Adding subscribers to mailchimp with bulk API V3
我正在尝试编写一个函数来在 mailchimp 中创建订阅者,但出现错误,我不明白哪里出了问题。
我正在使用 python-mailchimp(是的,我有正确的 list-id 而不是 000000)
我的代码:
from mailchimp3 import MailChimp
def export_to_mailchimp():
#Get all users in DB
users = User.objects.all()
#Create list for dictionaries
databody = []
#loop through queryset
for userobject in users:
#Create dictionary
databody_item = {
"email_address": item.email,
"status": "subscribed",
"merge_fields": {
"FNAME": item.first_name,
"LNAME": item.last_name
}
}
#Append to list
databody.append(databody_item)
client = MailChimp(settings.MAILCHIMP_USER, settings.MAILCHIMP_KEY)
batch = client.batches.create(data={"operations" : [{"method":"POST", "path":"/lists/00000/members/", "body":databody}]})
这是回溯:
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
.
这是正确的方法
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})
这是工作的:
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]
我猜应该是 post 方法的问题。有关更多详细信息,您可以使用下面 link.I 找到的内容:
https://devask.cz/questions/42175544/how-to-unsubscribe-delete-list-members-using-mailchimp3-in-python
我正在尝试编写一个函数来在 mailchimp 中创建订阅者,但出现错误,我不明白哪里出了问题。
我正在使用 python-mailchimp(是的,我有正确的 list-id 而不是 000000)
我的代码:
from mailchimp3 import MailChimp
def export_to_mailchimp():
#Get all users in DB
users = User.objects.all()
#Create list for dictionaries
databody = []
#loop through queryset
for userobject in users:
#Create dictionary
databody_item = {
"email_address": item.email,
"status": "subscribed",
"merge_fields": {
"FNAME": item.first_name,
"LNAME": item.last_name
}
}
#Append to list
databody.append(databody_item)
client = MailChimp(settings.MAILCHIMP_USER, settings.MAILCHIMP_KEY)
batch = client.batches.create(data={"operations" : [{"method":"POST", "path":"/lists/00000/members/", "body":databody}]})
这是回溯:
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
.
这是正确的方法
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})
这是工作的:
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]
我猜应该是 post 方法的问题。有关更多详细信息,您可以使用下面 link.I 找到的内容: https://devask.cz/questions/42175544/how-to-unsubscribe-delete-list-members-using-mailchimp3-in-python