10

I'm trying to access our MailChimp account via the new 3.0 REST API. I've done the following:

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

However, when I run this code, I get a 401 error with the following json details:

{"type":"http://kb.mailchimp.com/api/error-docs/401-api-key-invalid","title":"API Key Invalid","status":401,"detail":"Your API key may be invalid, or you've attempted to access the wrong datacenter.","instance":"a9fe4028-519e-41d6-9f77-d2caee4d4683"}

The datacenter I'm using in my URI (us1 in this example) matches the dc on my API key. My API key works if I use the MailChimp SDK so I know my key isn't invalid. Also, using Fiddler, I can see that the MailChimp SDK is calling the same dc as I'm doing in my URI.

Any Ideas as to why I am having trouble Authenticating?

EDIT As noted in the question, I'm asking specifically about accessing the new 3.0 REST API. I'm trying to do this directly as opposed to using a third party wrapper.

The new API is composed of http calls so it should be pretty straight forward. I'm simply having trouble with the authentication piece.

ekad
  • 14,436
  • 26
  • 44
  • 46
RHarris
  • 10,641
  • 13
  • 59
  • 103
  • possible duplicate of [How to integrate MailChimp in C#/.Net](http://stackoverflow.com/questions/5802667/how-to-integrate-mailchimp-in-c-net) – Mike Lawson May 20 '15 at 21:36

3 Answers3

18

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.

Nerdroid
  • 13,398
  • 5
  • 58
  • 69
RHarris
  • 10,641
  • 13
  • 59
  • 103
  • Incidentally, you can replace the word "Basic" with your username if you so desire in the AuthenticationHeaderValue constructor and things still work fine. – RHarris May 21 '15 at 17:58
  • This is actually only partially correct -- the way support offered does work, but the API's primary method of authentication is HTTP Basic Auth. Here's some sample .NET code that does that in a less hacky way: https://gist.github.com/bryanbarnard/8102915 – TooMuchPete May 22 '15 at 03:07
  • Hmm, your code looks almost identical to the non-working code I had originally posted. Do you see any reason why yours would work and mine would not? – RHarris May 22 '15 at 12:20
  • 1
    It looks like there might have been a bug that required the username to be 'apikey', but that was recently fixed. I'd give your original code another try and see if it works now. – TooMuchPete May 22 '15 at 18:04
  • 1
    Wow, you're right. Today the "non-working" code runs just fine. Yesterday that same code was failing all over the place. Thanks for the input. – RHarris May 22 '15 at 21:10
  • I tried your example, but I get errors that await can only be used within an async method. So, I started going backwards, making method after method async methods.. finally I came to a point which can not be passed, and I had to undo all that work. Do you have an example that isn't using async methods? We develop mission critical server events that can not run asynchronously. – James Sep 13 '16 at 20:56
  • When I test your code I get errors saying I need to make the containing scope async. Did you find a solution for this? – James Oct 19 '16 at 19:25
  • I think you could accomplish this by adding .Result to the end of the http call. (i.e.: `string content = http.GetStringAsync(@"...").Result;`) – RHarris Oct 20 '16 at 14:00
0

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);
Nerdroid
  • 13,398
  • 5
  • 58
  • 69
0

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

Sam Smith
  • 13
  • 3