gpt4 book ai didi

javascript - Axios POST 到 Mailchimp API : HTTP Authentication not working

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:19 27 4
gpt4 key购买 nike

我正在尝试向我的 mailchimp 帐户发出 axios 请求。但它似乎没有用。我无法让它授权我 - 我在这里做错了什么?我一直在关注this tutorial:其中说:

There are 2 authentication methods for the API: HTTP Basic authentication and OAuth2. The easiest way to authenticate is using HTTP Basic authentication. Enter any string as your username and supply your API Key as the password. Your HTTP client library should have built-in support for Basic authentication, but here’s a quick example that shows how to authenticate with the --user option in curl:

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 的东西并在这里随机列出名称)

我得到的错误:

OPTIONS https://us1.api.mailchimp.com/3.0/lists/afafaf/members/ 401 (Unauthorized) dispatchXhrRequest @ xhr.js:178 xhrAdapter @ xhr.js:12 dispatchRequest @ dispatchRequest.js:59 Promise resolved (async) request @ Axios.js:51 Axios.(anonymous function) @ Axios.js:71 https://us1.api.mailchimp.com/3.0/lists/afafaf/members/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 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=""

关于javascript - Axios POST 到 Mailchimp API : HTTP Authentication not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48790749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com