我们在使用MailChimp API V3将成员添加到列表时遇到问题。这是我们的代码:
client.BaseAddress = new Uri("https://us12.api.mailchimp.com/3.0/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", "API Key");
var reply = await client.GetStringAsync("lists");
var list = JsonConvert.DeserializeObject<ListResults>(reply);
AddMembers member = new AddMembers {
EmailAddress = "test@yahoo.com",
Status = "subscribed"
};
string url = "lists/" + list.Data[0].Id + "/members";
string jsonData = JsonConvert.SerializeObject(member);
var content = new StringContent(
jsonData,
Encoding.UTF8,
"application/json");
HttpResponseMessage addMember = await client.PostAsync(url, content);
这就是MailChimp所期望的:
--url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members'
--user 'anystring:apikey'
--header 'content-type: application/json'
--data '{"email_address":"urist.mcvankab+3@freddiesjokes.com", "status":"subscribed"}'
但是我们在下面的代码之后得到的是:
HttpResponseMessage addMember = await client.PostAsync(url, content);
是StatusCode为"400"的"错误请求"消息。我们真的很困惑,也许缺少了一些简单的东西。
如果这是客户端JavaScript,那就是问题所在。MailChimp的API不支持CORS,因此您将无法从浏览器发出API请求。在这种情况下,这可能是一件好事,因为将您的API密钥发送给客户可以让您网站的每一位访问者对您的MailChimp帐户拥有完全的管理员访问权限。