Mailchimp API Call using Ajax GET and access_token is throwing ERR_ABORTED 401

225 Views Asked by At

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.

0

There are 0 best solutions below