Gastón Saillén
Gastón Saillén

Reputation: 13129

Mailchimp error 400 request, can't send users to list

I'm working with Mailchimp and I want to send an email address to my list, I have done this so far with Volley:

public void suscribeMailChamp(){
    String listid = "listID";
    String url = "https://us16.api.mailchimp.com/3.0/lists/" + listid + "/members/";
    // Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            //
            Toast.makeText(MainActivity.this , "success" , Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(MainActivity.this , error.toString() , Toast.LENGTH_SHORT).show();
        }
    }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("email_address","[email protected]");
            params.put("status","unsubscribed");
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            params.put("Authorization" , "apikey <here my api key>");
            return params;
        }
    };
    queue.add(sr);
}

but I get error 400:

Unexpected response code 400 for https://us16.api.mailchimp.com/3.0/lists/b9a5943047/members/

Here is a link for troubleshooting but I can't get the error:

http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/

Upvotes: 1

Views: 1171

Answers (2)

Crennotech
Crennotech

Reputation: 519

I got stuck at this too. Mailchimp documentation is very poor. I found the solution from github

Just send 'Authorization': 'apikey myapikey' in your header

Upvotes: 0

Napster
Napster

Reputation: 1373

Did you check the error in the link? It says

{  
   "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
   "title":"API Key Missing",
   "status":401,
   "detail":"Your request did not include an API key.",
   "instance":"924c81cc-90e9-498d-b0fd-c7b54cba207f"
}

which means you are not (or correctly) sending the API key for mail chimp in the request. Just add the mailChimp API key in the params for Volley request


So the way you would send your API key is this

params.put("Authorization", "Basic "+Base64.encodeToString(("apikey:"+apiKey).getBytes("UTF-8"),Base64.DEFAULT))

Upvotes: 2

Related Questions