问题描述
我正在尝试通过 Twitter 进行护照身份验证以使其正常工作.我的设置如下:React(with redux) frontend (create-react-app), Node (with express) API.两者都在本地主机上运行(不同的端口).
用户转到 /login
并点击一个按钮来发送登录操作:
I'm trying to get passport authentication with twitter to work. My setup is as follows: React(with redux) frontend (create-react-app), Node (with express) API. Both are running on localhost (different ports).
User goes to /login
and clicks on a button that dispatches a login action:
export function login() {
return dispatch => {
return axios.get('/api/auth/twitter')
.then(() => {})
}
}
服务器上的路由:
router.get('/twitter', passport.authenticate('twitter'));
以及回调的路由:
router.get('/twitter/callback', passport.authenticate('twitter', {failureRedirect: '/login'}), loginSuccess);
loginSuccess
目前什么都不做.
这是我得到的完整错误:XMLHttpRequest 无法加载 https://api.twitter.com/oauth/authenticate?oauth_token=fGVctgAAAAAA1y22AAABXah3t3o.请求的资源上不存在Access-Control-Allow-Origin"标头.因此不允许访问源http://127.0.0.1:3000".
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.
我正在将来自 react 前端 (localhost:3000) 的请求代理到 localhost:8080.
I'm proxying requests from the react frontend (localhost:3000) to localhost:8080.
如果我进入 chrome devtools 的网络选项卡并从 twitter api 中选择响应,它看起来像这样:
If I go into the network tab of the chrome devtools and select the response from the twitter api it looks like this:
如果有人知道如何获得通行证 Twitter 身份验证以使用这种设置,请告诉我.
If anyone knows how to get passport-twitter authentication to work with this kind of setup please let me know.
推荐答案
悲伤的消息,Twitter 不允许你调用它的 API 客户端,你需要一个服务器来做到这一点:(
Sad news, Twitter won't let you to call its API client-side, you need a server in order to do that :(
看看这个 Quora 答案.
这篇关于请求的资源上不存在“Access-Control-Allow-Origin"标头.- 反应/节点 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!