Mailchimp RESTful API 3.0 HTTP Basic Auth
I am trying to use Mailchimp API version 3.0 with basic auth. I am using classic ASP.
The Json response is always "No API key".
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 am sending it as a header.
What am I doing wrong??
source to share
If you are trying to use Basic Auth then you need to follow the spec . You can create the header yourself using the wiki article as a guide, but the simplest is to just use your HTTP library's built-in support. 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 am using "apikey" as my 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
. It gives me YXBpa2V5Onh4eHh4eHh4eHgteXl5
. Now the generated header:
Authorization: Basic YXBpa2V5Onh4eHh4eHh4eHgteXl5
The method you are using will work, but is very common in MailChimp and can confuse future visitors to your code.
source to share
I see that the question was based on #C, but I had the same problem with java. (I'm new to java so couldn't edit my code).
public String get(String url) throws IOException {
HttpGet get = new HttpGet(url);
String apiEncode = "apikey:9590e52MyAPI8-us9";
String encoding = Base64.encodeBytes(apiEncode.getBytes());
get.setHeader("Authorization","Basic " + encoding );
HttpResponse response = http.execute(get);
if (response.getEntity() != null) {
return EntityUtils.toString(response.getEntity(), "UTF-8").trim();
} else {
throw new IOException(response.getStatusLine().toString());
}
}
source to share