user3759750
user3759750

Reputation: 153

MailChimp Integration in Java

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();

Upvotes: 0

Views: 5750

Answers (3)

Demobilizer
Demobilizer

Reputation: 748

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.

Upvotes: 0

freesoul
freesoul

Reputation: 246

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 :)

Upvotes: 4

duffymo
duffymo

Reputation: 308763

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.

Upvotes: 0

Related Questions