1
我試圖編寫一個函數來在mailchimp中創建訂閱者,但是我收到一個錯誤,我不明白是什麼錯誤。Python:使用批量API V3添加訂閱者到mailchimp
進出口使用python-mailchimp(是的,我有MYE正確的列表-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
。