3

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.

cris
  • 265
  • 3
  • 11
  • Are you using express on your server ? – Pawan Sharma Mar 08 '21 at 05:34
  • No, it just uses firebase and react router – cris Mar 08 '21 at 05:36
  • If your code is executed in a browser, CORS protections will automatically be in effect. The way to get around this is to make the API call from a server rather than a browser client. You could create your own API endpoint that redirects to the twitter API – Mackers Mar 08 '21 at 05:45

1 Answers1

1

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

Pawan Sharma
  • 1,842
  • 1
  • 14
  • 18