Mailchimp API 3.0 批量订阅 - Mailchimp3 PY
Posted
技术标签:
【中文标题】Mailchimp API 3.0 批量订阅 - Mailchimp3 PY【英文标题】:Mailchimp API 3.0 Batch subscribe - Mailchimp3 PY 【发布时间】:2019-05-16 23:24:55 【问题描述】:我是 Mailchimp api 的新手。我有这个包含订阅者列表的 csv 文件,需要将其批量导出到 mailchimp。我最初想在 for 循环中将每个迭代为 json 对象并进行 apic 调用,但这似乎不起作用。然后进一步阅读发现批处理操作。我正在尝试格式化每个批处理请求,但我收到一个错误:"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","status":401,"detail":"您的请求没有包括一个 API 密钥。","instance":"XXX"
import requests
import csv
import json
import glob
import os
from mailchimp3 import MailChimp
username = 'abc'
apikey = 'xxyyzzz'
url = "https://us12.api.mailchimp.com/3.0/lists/XXXX/members"
headers = 'Content-Type': 'application/json'
primary_fields = [ 'email_address','status']
result = []
for filename in glob.glob('D:\\abc.csv'):
csvfile = os.path.splitext(filename)[0]
jsonfile = csvfile + '.json'
with open('D:\\abc.csv', encoding="ISO-8859-1") as csv_file:
reader = csv.DictReader(csv_file, skipinitialspace=True)
for row in reader:
d = k: v for k, v in row.items() if k in primary_fields
d['merge_fields'] = k: v for k, v in row.items() if k not in primary_fields
result.append(d)
with open(jsonfile, 'w')as fp:
json.dump(result, fp, indent=2)
client = MailChimp(mc_api=apikey, mc_user= username)
with open("D:\\abc.json", "r") as read_file:
data= json.load(read_file)
#Create list for dictionaries
operations = []
#loop through queryset
for item in data:
#Create dictionary for a operation
operation_item = "method":"POST", "path":"/lists/XXXX/members/", "body":json.dumps(data)
#Append to list
operations.append(operation_item)
client = MailChimp(mc_api=apikey, mc_user= username)
batch = client.batches.create(data="operations": operations)
【问题讨论】:
【参考方案1】:标题中缺少授权字段。
headers = 'Content-Type': 'application/json', 'Authorization': 'Basic YOURAPIKEY'
【讨论】:
以上是关于Mailchimp API 3.0 批量订阅 - Mailchimp3 PY的主要内容,如果未能解决你的问题,请参考以下文章
Mailchimp api 3.0错误:“Schema描述对象,找到数组”是代码还是在mailchimp的结尾?
Mailchimp api 3.0 错误:“架构描述了对象,而是找到了数组”是代码还是在 mailchimp 的末尾?
Alamofire Mailchimp API 3.0 订阅