问题
Can someone please provide examples of doing this in ASP.NET. We want to do some MailCHimp – internal database synchronization and plan to do this using webhooks feature but we just can’t get it work. We want to use web hooks to synch data when someone unsubscribes from mail chimp.
Another thing to address is security. How can we secure this page from being accessed by malicious users?
回答1:
Here is a piece of code that works for us. This is fairly simple but it did take us some experimenting to get it to work.
if (Request.Form["type"] != null && Request.Form["type"] == "unsubscribe")
{
string email = Request.Form["data[merges][EMAIL]"];
//now you can do insert/update data in your local database
}
Check out the API documentation for more details http://apidocs.mailchimp.com/webhooks/
Regarding security you can do a ton of stuff but it depends on how deep you want to go. One thing I’d recommend is checking your IIS logs and finding which IP address/user agent is used by mail chimp to trigger web hooks and then just block this page for all other IP addresses except for this. There are probably other things you can do to additionally secure this like using page name that is not easily guessed (f3jijselife.aspx is far better than webhooks.aspx)
回答2:
I just implemented this recently based on the PHP code they provided here's the skeleton... I took out the actual implementation but should be useful hopefully
public class MailChimpWebHook : IHttpHandler
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(MailChimpWebHook));
private const string Key = "xxxx";
private const string ParamKey = "key";
private const string ParamType = "type";
private const string ParamListId = "data[list_id]";
private const string ParamListIdNew = "data[new_id]";
private const string ParamEmail = "data[email]";
private const string ParamOldEmail = "data[new_email]";
private const string ParamNewEmail = "data[old_email]";
private const string ParamProfileEmail = "data[merges][EMAIL]";
private const string ParamProfileFirstName = "data[merges][FNAME]";
private const string ParamProfileLastName = "data[merges][LNAME]";
private const string ParamProfileGroups = "data[merges][INTERESTS]";
private const string TypeSubscribe = "subscribe";
private const string TypeUnsubscribe = "unsubscribe";
private const string TypeCleaned = "cleaned";
private const string TypeUpdateEmail = "upemail";
private const string TypeProfileUpdate = "profile";
public void ProcessRequest(HttpContext context)
{
Logger.Info("==================[ Incoming Request ]==================");
if (string.IsNullOrEmpty(context.Request[ParamKey]))
{
Logger.Warn("No security key specified, ignoring request");
}
else if (context.Request[ParamKey] != Key)
{
Logger.WarnFormat("Security key specified, but not correct. Wanted: '{0}' | , but received '{1}'", Key, context.Request[ParamKey]);
}
else
{
//process the request
Logger.InfoFormat("Processing a '{0}' request...", context.Request[ParamType]);
try
{
switch (context.Request[ParamType])
{
case TypeSubscribe:
Subscribe(context.Request);
break;
case TypeUnsubscribe:
Unsubscribe(context.Request);
break;
case TypeCleaned:
Cleaned(context.Request);
break;
case TypeUpdateEmail:
UpdateEmail(context.Request);
break;
case TypeProfileUpdate:
UpdateProfile(context.Request);
break;
default:
Logger.WarnFormat("Request type '{0}' unknown, ignoring.", context.Request[ParamType]);
break;
}
}
catch (Exception e)
{
Logger.Error("There was an error processing the callback", e);
}
}
Logger.Info("Finished processing request.");
}
private void UpdateProfile(HttpRequest httpRequest)
{
Logger.Info("Processing update profile request!");
#region [ sample data structure ]
// "type": "profile",
// "fired_at": "2009-03-26 21:31:21",
// "data[id]": "8a25ff1d98",
// "data[list_id]": "a6b5da1054",
// "data[email]": "api@mailchimp.com",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "api@mailchimp.com",
// "data[merges][FNAME]": "MailChimp",
// "data[merges][LNAME]": "API",
// "data[merges][INTERESTS]": "Group1,Group2",
// "data[ip_opt]": "10.20.10.30"
#endregion
}
private void UpdateEmail(HttpRequest httpRequest)
{
Logger.Info("Processing update email request!");
#region [ sample data structure ]
// "type": "upemail",
// "fired_at": "2009-03-26\ 22:15:09",
// "data[list_id]": "a6b5da1054",
// "data[new_id]": "51da8c3259",
// "data[new_email]": "api+new@mailchimp.com",
// "data[old_email]": "api+old@mailchimp.com"
#endregion
}
private void Cleaned(HttpRequest httpRequest)
{
Logger.Info("Processing cleaned email request!");
#region [ sample data structure ]
// "type": "cleaned",
// "fired_at": "2009-03-26 22:01:00",
// "data[list_id]": "a6b5da1054",
// "data[campaign_id]": "4fjk2ma9xd",
// "data[reason]": "hard",
// "data[email]": "api+cleaned@mailchimp.com"
#endregion
}
private void Unsubscribe(HttpRequest httpRequest)
{
Logger.Info("Processing unsubscribe...");
#region [ sample data structure ]
// "type": "unsubscribe",
// "fired_at": "2009-03-26 21:40:57",
// "data[action]": "unsub",
// "data[reason]": "manual",
// "data[id]": "8a25ff1d98",
// "data[list_id]": "a6b5da1054",
// "data[email]": "api+unsub@mailchimp.com",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "api+unsub@mailchimp.com",
// "data[merges][FNAME]": "MailChimp",
// "data[merges][LNAME]": "API",
// "data[merges][INTERESTS]": "Group1,Group2",
// "data[ip_opt]": "10.20.10.30",
// "data[campaign_id]": "cb398d21d2",
// "data[reason]": "hard"
#endregion
}
private void Subscribe(HttpRequest httpRequest)
{
Logger.Info("Processing subscribe...");
#region [ sample data structure ]
// "type": "subscribe",
// "fired_at": "2009-03-26 21:35:57",
// "data[id]": "8a25ff1d98",
// "data[list_id]": "a6b5da1054",
// "data[email]": "api@mailchimp.com",
// "data[email_type]": "html",
// "data[merges][EMAIL]": "api@mailchimp.com",
// "data[merges][FNAME]": "MailChimp",
// "data[merges][LNAME]": "API",
// "data[merges][INTERESTS]": "Group1,Group2",
// "data[ip_opt]": "10.20.10.30",
// "data[ip_signup]": "10.20.10.30"
#endregion
}
public bool IsReusable
{
get
{
return false;
}
}
}
回答3:
I'm using C# WebAPI and the solution for me was to use the FormDataCollection object from the body of the POST MailChimp sends with the webhook.
using System.Net.Http.Formatting;
[HttpPost]
[Route("mailchimp/subscriber")]
public IHttpActionResult Post([FromBody] FormDataCollection data)
{
if (data != null)
{
string type = data.Get("type");
if (!string.IsNullOrWhiteSpace(type))
{
string listId = data.Get("data[list_id]");
string id = data.Get("data[id]");
string firstName = data.Get("data[merges][FNAME]");
string lastName = data.Get("data[merges][LNAME]");
string email = data.Get("data[email]");
if (!string.IsNullOrWhiteSpace(email))
{
// Do something with the subscriber
}
}
}
}
回答4:
I fully support the answer by James.
However, when trying to implement a webhook myself, I have discovered that you'll also need to implement a GET method, in order to even be able to create the webhook in MailChimp.
This did the trick for me:
[HttpGet]
[HttpOptions]
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK);
}
MailChimp documentation: https://developer.mailchimp.com/documentation/mailchimp/guides/about-webhooks/
来源:https://stackoverflow.com/questions/15020807/using-mailchimp-webhooks-feature