Access MailChimp API 3.0 (GET)

喜夏-厌秋 提交于 2020-02-04 07:29:47

问题


I'm trying to make a GET request through jQuery to the Mailchimp API. It seems though my custom header is not correctly set as I get a Your request did not include an API key. error.

It works fine if I make the request using curl on my Ubuntu machine:

curl --header "Authorization: apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11" https://us11.api.mailchimp.com/3.0/campaigns

Here's my code:

$.ajax({
    type: 'GET',
    url: 'https://us11.api.mailchimp.com/3.0/campaigns',
    crossDomain: true,
    dataType: 'jsonp',
    contentType: "application/json; charset=utf-8",
    headers: {
        'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11'
    }
}).done(function (response) {
    console.log(response); // verbose
});

I even tried adding this above:

$.ajaxSetup({
    headers: { 'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11' }
});

回答1:


You need to add the key via Basic Auth like and as far I am aware off, You can't query it from front-end, it must be on the back-end.

Find an example in NodeJS:

headers: {
    'Authorization': 'Basic ' + new Buffer(`anything:${MailChimpKey}`).toString('base64');
}



回答2:


MailChimp not allowed to direct access with ajax. Once make Server WebRequest. It will surely work.



来源:https://stackoverflow.com/questions/31324649/access-mailchimp-api-3-0-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!