[英]Axios POST to Mailchimp API: HTTP Authentication not working
我正在尝试向我的mailchimp帐户发出axios请求。 但它似乎没有奏效。 我不能让它授权我 - 我在这里做错了什么? 我一直在关注本教程:其中说:
API有两种身份验证方法:HTTP基本身份验证和OAuth2。 最简单的身份验证方法是使用HTTP基本身份验证。 输入任何字符串作为您的用户名,并提供您的API密钥作为密码。 您的HTTP客户端库应该具有对基本身份验证的内置支持,但是这是一个快速示例,显示如何使用curl中的--user选项进行身份验证:
curl --request GET \
--url 'https://<dc>.api.mailchimp.com/3.0/' \
--user 'anystring:<your_apikey>'
所以我实施了:
axios.post('https://us1.api.mailchimp.com/3.0/lists/xad81287/members/', {
auth:
{
url: 'https://us1.api.mailchimp.com/3.0',
user: 'blabla:11231h23123j14bhj1b23j12-us1' //this is my API key
},
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
(我在这里随机更改了API的东西和列表名称)
我得到的错误:
选项https://us1.api.mailchimp.com/3.0/lists/afafaf/members/ 401(未经授权)dispatchXhrRequest @ xhr.js:178 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:59 Promise已解决(异步)请求@Axios.js:51 Axios。(匿名函数)@Axios.js:71 https://us1.api.mailchimp.com/3.0/lists/afafaf/members/ :对预检请求的响应未通过访问控制检查:请求的资源上没有“Access-Control-Allow-Origin”标头。 因此不允许来源' http:// localhost:8000 '访问。 响应具有HTTP状态代码401。
尝试这个:
axios({
method:'post',
url: 'https://us1.api.mailchimp.com/3.0/lists/xad81287/members/',
auth: {
username: 'Fred', // anystring
password: '11231h23123j14bhj1b23j12-us1' // Your API key
}
}).then(response => console.log(response)).catch(error => console.log(error))
它对我有用。
对于使用localhost
Mailchimp API(因此没有CORS错误)退出Google Crome并使用以下命令打开它:
open -a Google\ Chrome --args --disable-web-security --user-data-dir=""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.