[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. - React/Node API
I'm trying to get passport authentication with twitter to work. 我正在尝试使用Twitter进行护照认证。 My setup is as follows: React(with redux) frontend (create-react-app), Node (with express) API. 我的设置如下:React(带有redux)前端(create-react-app),Node(带有express)API。 Both are running on localhost (different ports). 两者都在localhost(不同的端口)上运行。
User goes to /login
and clicks on a button that dispatches a login action: 用户转到/login
并单击调度登录操作的按钮:
export function login() {
return dispatch => {
return axios.get('/api/auth/twitter')
.then(() => {})
}
}
The route on the server: 服务器上的路由:
router.get('/twitter', passport.authenticate('twitter'));
And the route for the callback: 以及回调的路线:
router.get('/twitter/callback', passport.authenticate('twitter', {failureRedirect: '/login'}), loginSuccess);
loginSuccess
does nothing at the moment. loginSuccess
目前不执行任何操作。
This is the full error I get: 这是我得到的完整错误:
XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=fGVctgAAAAAA1y22AAABXah3t3o. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000' is therefore not allowed access.
I'm proxying requests from the react frontend (localhost:3000) to localhost:8080. 我正在将请求从React前端(localhost:3000)代理到localhost:8080。
If I go into the network tab of the chrome devtools and select the response from the twitter api it looks like this: 如果我进入chrome devtools的网络标签,然后从twitter api中选择响应,则如下所示:
If anyone knows how to get passport-twitter authentication to work with this kind of setup please let me know. 如果有人知道如何通过护照设置者身份验证来使用这种设置,请告诉我。
Sad news, Twitter won't let you to call its API client-side, you need a server in order to do that :( 不幸的消息是,Twitter不允许您调用其API客户端,您需要一台服务器才能做到这一点:(
Take a look at this Quora answer . 看看这个Quora的答案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.