简体   繁体   English

所请求的资源上没有“ Access-Control-Allow-Origin”标头。 -React / Node API

[英]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.

相关问题 **请求的资源上不存在“Access-Control-Allow-Origin”header。** - **No 'Access-Control-Allow-Origin' header is present on the requested resource.** 请求的资源上不存在“Access-Control-Allow-Origin”header。 节点快速获取 - No 'Access-Control-Allow-Origin' header is present on the requested resource. Node Express Fetch 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ localhost:8080” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8080' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”header。 节点服务器,我已将原点应用于 Cors - No 'Access-Control-Allow-Origin' header is present on the requested resource. NODE-server, I have applied an origin to Cors 请求的资源上不存在“Access-Control-Allow-Origin”标头。 NodeJS 和 AngularJS 应用 - No 'Access-Control-Allow-Origin' header is present on the requested resource. NodeJS and AngularJS App Access-Control-Allow-Origin'标头存在于请求的资源上。 错误 - Access-Control-Allow-Origin' header is present on the requested resource. error ReactJs - NextJs 错误 - (请求的资源上不存在“访问控制允许来源”header。) - ReactJs - NextJs Error - (No 'Access-Control-Allow-Origin' header is present on the requested resource.) 请求的资源上不存在“Access-Control-Allow-Origin”标头。 AWS Lambda - No 'Access-Control-Allow-Origin' header is present on the requested resource. AWS Lambdas 请求的资源上不存在“Access-Control-Allow-Origin”header。 NodejS Express - No 'Access-Control-Allow-Origin' header is present on the requested resource. NodejS Express fetch: o 'Access-Control-Allow-Origin' header 出现在请求的资源上:react 和 node - fetch : o 'Access-Control-Allow-Origin' header is present on the requested resource : react and node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM