0

I want to integrate MailChimp API in my java project. When I call Rest call using HttpURLConnection class, it responds with 401 code.

Here is my code:

URL url = new URL("https://us13.api.mailchimp.com/3.0/lists");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "apikey <my-key>");

String input = "<json data>";

OutputStream os = conn.getOutputStream();
//os.write(input.getBytes());
os.flush();

if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
    throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
    System.out.println(output);
}

conn.disconnect();
ekad
  • 14,436
  • 26
  • 44
  • 46
user3759750
  • 123
  • 3
  • 10

3 Answers3

4

I will suggest using Apache Commons Codec package for encoding. It support various formats such as Base64 and Hexadecimal.

Earlier I was also facing the same issue. I am sharing the code that I used in my application for authenticating to Mailchimp API v-3.0

//basic imports
import org.apache.commons.codec.binary.Base64;
.
.
.
 //URL to access and Mailchimp API key 
String url = "https://us9.api.mailchimp.com/3.0/lists/";
//mailchimp API key 
String apikey = xxxxxxxxxxxxxxxxxxxxxxxxxxx

// Authentication PART

String name = "Anything over here!";
String password = apikey;     //Mailchimp API key
String authString = name + ":" + password;

byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);

URL urlConnector = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection)           urlConnector.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setDoOutput(true);
httpConnection.setDoInput(true);
httpConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);

InputStream is1 = httpConnection.getInputStream();
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is1, "utf-8"));

String line = null;
while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
            }
br.close();

Now you can use StringBuilder Object sb to parse the output as required

Hope it resolves your issue :)

freesoul
  • 236
  • 1
  • 13
0

HTTP 401 response code means "not authorized".

You didn't set or pass your credentials properly. Is the certificate from the client set up? Here's an example of an HTTPS client.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Hi, Thanks for your prompt reply. Yes, I know the problem is in the way of passing api key. I checked the MailChimp api and they don't have any example on how to pass api key in Java to authorize. – user3759750 Jun 14 '16 at 12:59
  • It's HTTP, so my first guess would be to set the HTTP basic auth headers. – duffymo Jun 14 '16 at 13:01
  • MailChimp API uses Basic Authentication. There are tons of java examples of that out in the world, like [this one](http://stackoverflow.com/questions/4883100/how-to-handle-http-authentication-using-httpurlconnection). – TooMuchPete Jun 14 '16 at 21:34
  • I tried those things but still it returns with 401 status. I am not sure if I am missing anything for MailChimp api to pass. – user3759750 Jun 15 '16 at 11:13
0

HTTP 401 simply means you're not Authorized to send this request.

you can set username any string (the MailChimp docs suggest using anystring as a username) and your API key as a password.

In case of Postman request, you can set under the Authorization tab choose Basic Auth to set username and password. Below image shows the same.

enter image description here

More info about Adding/ Getting Members to/ from a Mailing List on MailChimp API 3.0, I find this article very useful.

Demobilizer
  • 663
  • 9
  • 12