APIv3-examples icon indicating copy to clipboard operation
APIv3-examples copied to clipboard

Error 401 (Unauthorized) when sending a request for adding a new list member

Open SilencerWeb opened this issue 7 years ago • 15 comments

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 protected]',
        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?

SilencerWeb avatar Feb 21 '18 09:02 SilencerWeb

Try 'Authorization': 'apikey 8ec49e64f2041073d3fe56e0abc5fe5f-us17'

antonkorotkov avatar Oct 19 '18 12:10 antonkorotkov

Doesn't work for me with the same problem.

davidbernat avatar Jan 30 '19 17:01 davidbernat

I also have same problem

myside1 avatar Jun 17 '19 13:06 myside1

I'm having the same issue, no matter what I do the request won't be sent with the Authorization header value, if I edit and resend the request with the Auth header added it works.

fetch(url, { // method: 'POST', // mode: 'no-cors', // credentials: 'same-origin', // headers: new Headers({ // 'Authorization': 'apikey XXXXXXXXXXXXXXXXXXXXXXXXX-us12', // 'Content-Type': 'application/ json' // }), // body: JSON.stringify(data) // });

samroberts707 avatar Oct 21 '19 15:10 samroberts707

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.

ashleymazzonna avatar Jan 20 '20 00:01 ashleymazzonna

Any updates? How to do the right request?

let authenticationString = btoa('user:API_KEY-us20'); authenticationString = Basic ${authenticationString};

const result = await fetch('https://us20.api.mailchimp.com/3.0/lists/XXXXXXXX/members', {
  mode: 'no-cors',
  method: 'POST',
  headers: {
    authorization: authenticationString,
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email_address: '[email protected]',
    status: 'subscribed',
  }),
  auth: {
    user: 'user',
    pass: 'API_KEY-us20',
  },
});

necheporenko avatar Apr 02 '20 14:04 necheporenko

Any updates? How to do the right request?

let authenticationString = btoa('user:API_KEY-us20'); authenticationString = Basic ${authenticationString};

const result = await fetch('https://us20.api.mailchimp.com/3.0/lists/XXXXXXXX/members', {
  mode: 'no-cors',
  method: 'POST',
  headers: {
    authorization: authenticationString,
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email_address: '[email protected]',
    status: 'subscribed',
  }),
  auth: {
    user: 'user',
    pass: 'API_KEY-us20',
  },
});

Hey. Did you try to make your Authorization header look like here? https://github.com/mailchimp/APIv3-examples/issues/41#issuecomment-431344785

antonkorotkov avatar Apr 02 '20 14:04 antonkorotkov

Any updates? How to do the right request? let authenticationString = btoa('user:API_KEY-us20'); authenticationString = Basic ${authenticationString};

const result = await fetch('https://us20.api.mailchimp.com/3.0/lists/XXXXXXXX/members', {
  mode: 'no-cors',
  method: 'POST',
  headers: {
    authorization: authenticationString,
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email_address: '[email protected]',
    status: 'subscribed',
  }),
  auth: {
    user: 'user',
    pass: 'API_KEY-us20',
  },
});

Hey. Did you try to make your Authorization header look like here? #41 (comment)

Hey, yeah, but unfortunately it didn't help me. It works perfectly on the Postman, but not on my react app.

I guess this simple request should work on the Browser console, but not.

 const result = await fetch('https://us20.api.mailchimp.com/3.0', {
      mode: 'no-cors',
      headers: {
        Authorization: 'apikey MY-API-us20',
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    });

necheporenko avatar Apr 02 '20 16:04 necheporenko

Any updates? How to do the right request? let authenticationString = btoa('user:API_KEY-us20'); authenticationString = Basic ${authenticationString};

const result = await fetch('https://us20.api.mailchimp.com/3.0/lists/XXXXXXXX/members', {
  mode: 'no-cors',
  method: 'POST',
  headers: {
    authorization: authenticationString,
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email_address: '[email protected]',
    status: 'subscribed',
  }),
  auth: {
    user: 'user',
    pass: 'API_KEY-us20',
  },
});

Hey. Did you try to make your Authorization header look like here? #41 (comment)

Hey, yeah, but unfortunately it didn't help me. It works perfectly on the Postman, but not on my react app.

I guess this simple request should work on the Browser console, but not.

 const result = await fetch('https://us20.api.mailchimp.com/3.0', {
      mode: 'no-cors',
      headers: {
        Authorization: 'apikey MY-API-us20',
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
    });

Ah, you actually should not do this in the browser. I thought it is node code. Making it in the browser exposes you API Key that is not what you want I guess. Instead, you should proxy your request using some backend handler which will, in turn, communicate with the Mailchimp servers using PHP, nodejs or whatever your backend is. In other words, Mailchip just does not allow doing direct requests from the browser. Postman works because it is basically not a browser.

antonkorotkov avatar Apr 03 '20 06:04 antonkorotkov

I had the same issue for a long

Sara-2007 avatar Jun 07 '20 00:06 Sara-2007

But then I realized it was an issue with mailchimp

Sara-2007 avatar Jun 07 '20 00:06 Sara-2007

So instead I used Firebase by google

Sara-2007 avatar Jun 07 '20 01:06 Sara-2007

It worked way better than Mailchimp

Sara-2007 avatar Jun 07 '20 01:06 Sara-2007

Here is the link: https://firebase.google.com/

Sara-2007 avatar Jun 07 '20 01:06 Sara-2007

Hope this helps!

Sara-2007 avatar Jun 07 '20 01:06 Sara-2007