Twitter API authorization fails CORS preflight in browser

只谈情不闲聊 提交于 2019-11-27 09:26:47

So the unsatisfying resolution appears to be that the Twitter API does not support CORS. This seems a little astonishing to me, as it means that the API cannot be used from a browser.

That policy decision is probably related to their OAuth implementation, which is vulnerable to anyone with access to the calling platform. Maybe that was okay back in 2010, but most of the other major internet players have figured out how to do client-based authorization.

A workaround and it is a long way around is to build a proxy server that you run with node or something else, I've done this a few times now, this is a good starter repo for someone running into this issue. It has the downside of being React specific, but you could always rip out the react ui and just setup with whatever you need: hcra twitter build.

Its forked from a Create React App/Node Express boilerplate by Mars Hall

You will need to clone it and run git fetch and then checkout twitter-req branch.

One workaround would be to create a NodeJS server for the same and call the Node API requesting Twitter API from front-end.

As I cannot comment, check this workaround: https://stackoverflow.com/a/43881141

In short, prefix the request so the full URI looks something like this:

https://cors-anywhere.herokuapp.com/https://api.twitter.com/oauth/request_token

Unfortunately, you will not be able to use most Twitter libraries this way, but you can continue using fetch with the cors-anywhere prefix.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!