Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailchimp RESTful API 3.0 HTTP Basic Auth

I'm trying to use the Mailchimp API version 3.0 with basic auth. I'm using Classic ASP.

The Json response is always: "API Key Missing".

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.open "GET", "https://us4.api.mailchimp.com/3.0/", False
HttpReq.setRequestHeader "Content-Type", "application/json"
HttpReq.setRequestHeader "apikey", "xxxxxx"
HttpReq.send ""
Response.Write  HttpReq.ResponseText
Set HttpReq = Nothing

I'm sending it as a header.

What am I doing wrong..?

like image 827
Stephen Last Avatar asked Jun 19 '15 10:06

Stephen Last


2 Answers

The answer is:

HttpReq.setRequestHeader "Authorization", "apikey xxxxxx"
like image 116
Stephen Last Avatar answered Sep 24 '22 18:09

Stephen Last


If you're trying to use Basic Auth, you need to actually follow the spec. You can build the header yourself using the wiki article as your guide, but the easiest thing is to just use your HTTP Library's built-in support for that. In your case, this will probably help.

To roll your own, you need two pieces of information. The first is the username the second is the password. For MailChimp v3.0, the username can be anything. I tend to use 'apikey' as the username. The password is the API Key itself. Let's say my API Key is 'xxxxxxxxxx-yyy'. Now, you Base 64 Encode the string apikey:xxxxxxxxxx-yyy. That gives me YXBpa2V5Onh4eHh4eHh4eHgteXl5. Now the header I create is:

Authorization: Basic YXBpa2V5Onh4eHh4eHh4eHgteXl5

The method you're using will work, but is very custom to MailChimp and might confuse future visitors to your code.

like image 42
TooMuchPete Avatar answered Sep 22 '22 18:09

TooMuchPete