How to solve “Getting blocked by CORS policy” when using twitter API in nodejs?

I have this block of code that I’m testing to see if it’ll send a tweet if it’s button is clicked.

  const onTweet = () => {
    client.post('statuses/update', {status: 'Testing'})
    .then(function (tweet) {
      console.log(tweet);
    })
    .catch(function (error) {
      console.log(error + " error")
    });
  }

However, every time I click on the button I get this exact error

Access to fetch at 'https://api.twitter.com/1.1/statuses/update.json' from origin 'http://localhost:1234' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I’ve tried to research online to see how to fix it or work around it, but I don’t think the solution I’ve found apply to my problem. For example, this post Access to fetch at from origin ‘http://localhost:3000’ has been blocked by CORS policy, says to set the mode to no cors but I don’t think that applies to my problem since I’m not using CORS to post this tweet. This is in nodeJS and the backend was built with firebase and react router dom.

Also, if someone can link the official twitter api nodejs documentation I’d appreciate it. Haven’t found it if there is any.

Answer

It looks like twitter API doesn’t have CORS support . You may try workarounds like extensions or Postman/Curl etc as mentioned here –

https://stackoverflow.com/a/35898961/7895283

https://twittercommunity.com/t/will-twitter-api-support-cors-headers-soon/28276/6

Leave a Reply

Your email address will not be published. Required fields are marked *