在MailChimp API V3中订阅用户

时间:2015-12-19 18:17:57

标签: c# json.net mailchimp mailchimp-api-v3.0

使用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'的'错误请求'消息。我们真的很困惑,也许缺少一些简单的东西。

0 个答案:

没有答案