Calling MailChimp API v3.0 with .Net

跟風遠走 提交于 2019-11-28 23:50:59
RHarris

So I was able to finally chat with a super tech support person at MailChimp.

The MailChimp docs state the following

The easiest way to authenticate is using HTTP Basic Auth. Enter any string as the username and supply your API Key as the password. Your HTTP library should have built-in support for basic authorization.

Their documentation is a bit misleading. Typically the Auth header for Basic Auth would look like what I was sending:

Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx where the row of x would represent the base64 encoded username:password.

However, talking with the support tech, the actual implementation they use is:

Authorization: username keyid

No base64 encoding, no Basic keyword. Username doesn't even have to be your username.

So, here is the working code:

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

EDIT Note the comments. TooMuchPete was correct in that the normal HTTP Basic Auth headers do work. Apparently I was hitting some old code or something on the MailChimp side.

I'm leaving the post as a reference for anyone who is trying to call the new 3.0 API.

I wrote an article on a simple way up adding subscribers to a list using:

Dim mailchimp As New ZmailChimp
Dim ListId$ = "9b2e63f0b9"   'List Sage' List
Dim email$ = "samsmith20@anymail.com" '"sam19@postcodelite.com"
Dim fieldListOnAdd = "FNAME,Sam,LNAME,Smith,MTYPE,User,MID,631637"
Dim fieldListOnUpdate = "FNAME,Sam,LNAME,Smith,MID,631637"  'Don't change MTYPE
'Put on 'Sage One' and 'Sage 50' group
Dim groupList = "407da9f47d,05086211ba"

With mailchimp
     .API$ = "46cMailChimpAPIKeyd1de-us14" 'MailChimp API key
     .dataCenter$ = "us14"  'Last 4 letters of API key
     .password$ = "Password!"
     MsgBox(.addSubscriber(ListId$, email, fieldListOnAdd, fieldListOnUpdate, groupList))
End With
mailchimp = Nothing

see:http://www.codeproject.com/Tips/1140339/Mail-Chimp-Add-Update-e-mail-to-List-and-Subscribe
this may save someone some time

Mohsinali Momin

Mailchimp Ecommerce

var mcorder = new Standup.Ecomm.MailChimpManager(ConfigurationManager.AppSettings["MailChimpApiKey"]);
var orders = new MailOrder();

orders.CampaignId = ConfigurationManager.AppSettings["MailChimpCampaignId"];
orders.EmailId = ConfigurationManager.AppSettings["MailChimpEmailId"];

orders.Id = orderNumber;
orders.StoreId = "abcde";
orders.StoreName = "E-Commerce Store";
orders.Total = Convert.ToDouble(orderTotal);
orders.Tax = Convert.ToDouble(tax);
orders.Items = new List<MailOrderItem>();
foreach (var orderItem in orderItemList)
{
    var item = new MailOrderItem();
    item.ProductId = orderItem.OrderNumber;
    item.ProductName = orderItem.Title;
    item.SKU = orderItem.Sku;
    item.CategoryId = 0;
    item.CategoryName = " ";
    item.Quantity = orderItem.Quantity;
    item.Cost = Convert.ToDouble(orderItem.ProductCost);
    orders.Items.Add(item);
}
mcorder.AddOrder(orders);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!