gpt4 book ai didi

python - Mailchimp API 3.0 批量订阅 - Mailchimp3 PY

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:22 24 4
gpt4 key购买 nike

我是 Mailchimp api 的新手。我有这个 csv 文件,其中包含订阅者列表,需要批量导出到 mailchimp。我最初想在 for 循环中将每个迭代为 json 对象并进行 apic 调用,但这似乎不起作用。然后在进一步阅读中发现了批处理操作。我正在尝试按批处理请求格式化,但出现错误:{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ ","title":"API Key Missing","status":401,"detail":"Your request did not包含一个 API key 。","实例":"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})

最佳答案

header 中缺少授权字段。

headers = {'Content-Type': 'application/json', 'Authorization': 'Basic YOURAPIKEY'}

关于python - Mailchimp API 3.0 批量订阅 - Mailchimp3 PY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53800919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com