0

I am working on a project, where Mailchimp API needs to be integrated and there is no backend is involved. All the calls are made from the frontend JS/jQuery.

I got the access_token using authorize_uri, access_token_uri & redirect_uri and saved the 'access_token' to the cookies.

Now I made an API call using Ajax GET to the 'campaign' resources but got an error.

My Ajax call is,

       $.ajax({
            url: camp_url,
            dataType: 'jsonp',
            type: 'POST',
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Content-Type":"application/json",
                "Accept": "application/json",
                //"Authorization": "OAuth oauth_token=ACCESSTOKEN"
            },
            beforeSend : function( xhr ) {
                xhr.setRequestHeader( "Authorization", "BEARER " + access_token );
            },
            success: function(result){
                console.log(result);
            },
            error : function(error) {
                console.log(error);
            }
        });

The error I got is,

"GET https://us5.api.mailchimp.com/3.0/campaigns?callback=jQuery341024416319697396371_1574104379602&_=1574104379603 net::ERR_ABORTED 401"

The JSON from the error link is,

{
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: "a1763594-cc49-431f-bc95-25560a24605d"
}

I played around with the 'headers' but to no use. Please help. Thanks in advance.

Note: When I used 'Postman' with the resource URL and acess_token(Authorisation), it successfully returned the JSON data.

SangyK
  • 799
  • 2
  • 6
  • 16
  • 1
    The error you are getting tells you exactly what the problem is: *Your request did not include an API key*. Typically you must pass the auth info as http headers or query parameters. However a bit of searching [suggests you can't do this in JS now](https://stackoverflow.com/questions/32778133/mailchimp-api-v3-jquery-ajax-post-subscribers), due to CORS restrictions. Maybe that's why you were trying to use the session, but [check this comment](https://stackoverflow.com/questions/32778133/mailchimp-api-v3-jquery-ajax-post-subscribers#comment53453846_32785851) for why you should not do that. – Don't Panic Nov 19 '19 at 08:32
  • Thanks. Yes, Mailchimp doesn't allow the client-side implementation of API. So we are going to try using CORS proxy in our system. – SangyK Dec 12 '19 at 07:37

0 Answers0