问题
Here is how I'm trying to send a request:
fetch('https://us17.api.mailchimp.com/3.0/lists/185415c92c/members', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'anystring:8ec49e64f2041073d3fe56e0abc5fe5f-us17',
},
body: JSON.stringify({
email_address: 'email@mail.ru',
status: 'subscribed',
}),
mode: 'no-cors',
})
And I get such error:
POST https://us17.api.mailchimp.com/3.0/lists/185415c92c/members 401 (Unauthorized)
What am I doing wrong?
回答1:
The Authorization
header contains both type and credentials, but in your code the header only contains the latter. The following should work:
Authorization: Basic <base64-encoding of anystring:8ec49e64f2041073d3fe56e0abc5fe5f-us17>
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
EDIT: The real reason why it wasn't working: CORS, which MailChimp understandably doesn't support.
回答2:
I kept having the same issue, I checked my Key from MailChimp in my account setting. Under KEY, it said my key "was disabled" and under LABEL it said "posted in public, do not enable". Sometimes it's not our code, it's the key itself. Try checking the key you are currently using.
来源:https://stackoverflow.com/questions/48903714/mailchimp-error-401-unauthorized-when-sending-a-request-for-adding-a-new-list