gpt4 book ai didi

c# - 在 ASP.NET 中将订阅者添加到 Mail Chimp 3.0 中的列表

转载 作者:太空狗 更新时间:2023-10-29 21:21:56 25 4
gpt4 key购买 nike

我正在尝试使用我的 ASP.NET C# 网站实现 Mail Chimp 的新 API,因此当用户在输入框中输入他们的电子邮件地址时,它将自动添加到我的 mailchimp 列表中。我尝试了各种其他方法,但均无效。

到目前为止,我的代码详述如下:

public bool BatchSubscribe(IEnumerable<MailChimpSubscriberModel> newSubscribers)
{
if (string.IsNullOrEmpty(_defaultListId)) throw new ArgumentNullException(Res.Resource.MailChimpIntegrationNoListId);

if (string.IsNullOrEmpty(_apiKey)) throw new ArgumentNullException(Res.Resource.MailChimpIntegrationNoApiKey);

foreach (MailChimpSubscriberModel subscriber in newSubscribers)
{
string url = "https://" + _dataPoint + ".api.mailchimp.com/3.0/lists/" + _defaultListId + "/";

Subscriber member = new Subscriber();
member.email = subscriber.Email;
member.subscribed = "subscribed";

string jsonString = new JavaScriptSerializer().Serialize(member);

//using (WebClient client = new WebClient())
//{
// client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
// client.Headers[HttpRequestHeader.Authorization] = new AuthenticationHeaderValue("Basic", _apiKey).ToString();
// string HtmlResult = client.(url, jsonString);

// return true;
//}

using (var http = new HttpClient())
{
http.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", _apiKey);
string content = await http.**GetStringAsync**(@"https://us11.api.mailchimp.com/3.0/lists");
Console.WriteLine(content);
}
}
return false;
}

最佳答案

这个问题我有点晚了,但是我花了半天的时间才弄明白,这里有我的答案,所以它可以帮助别人。我正在使用 MailChimp 3.0:

private void subscribeAddress() 
{
string apiKey = "APIKEY-usX"; //your API KEY created by you.
string dataCenter = "usX";
string listID = "listID"; //The ListID of the list you want to use.

SubscribeClassCreatedByMe subscribeRequest = new SubscribeClassCreatedByMe
{
email_address = "somebodys@email.com",
status = "subscribed"
};
subscribeRequest.merge_fields = new MergeFieldClassCreatedByMe();
subscribeRequest.merge_fields.FNAME = "YourName";
subscribeRequest.merge_fields.LNAME = "YourSurname";

using (HttpClient client = new HttpClient())
{
var uri = "https://" + dataCenter + ".api.mailchimp.com/";
var endpoint = "3.0/lists/" + listID + "/members";

client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", apiKey);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.BaseAddress = new Uri(uri);

//NOTE: To avoid the method being 'async' we access to '.Result'
HttpResponseMessage response = client.PostAsJsonAsync(endpoint, subscribeRequest).Result;//PostAsJsonAsync method serializes an object to
//JSON and then sends the JSON payload in a POST request
//StatusCode == 200
// -> Status == "subscribed" -> Is on the list now
// -> Status == "unsubscribed" -> this address used to be on the list, but is not anymore
// -> Status == "pending" -> This address requested to be added with double-opt-in but hasn't confirmed yet
// -> Status == "cleaned" -> This address bounced and has been removed from the list

//StatusCode == 404
if ((response.IsSuccessStatusCode))
{
//Your code here
}
}
}

这里有类 SubscribeClassCreatedByMe 和 MergeFieldClassCreatedByMe

namespace Subscriber
{
public class SubscribeClassCreatedByMe
{
public string email_address { get; set; }
public string status { get; set; }
public MergeFieldClassCreatedByMe merge_fields { get; set; }
}
}
namespace Subscriber
{
public class MergeFieldClassCreatedByMe
{
public string FNAME { get; set; }
public string LNAME { get; set; }
}
}

嗯,我希望这对你有所帮助!!

关于c# - 在 ASP.NET 中将订阅者添加到 Mail Chimp 3.0 中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31991398/

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