[英]Origin 'http://localhost:3000' is therefore not allowed access Twitter API
I'm developing a web application that uses the Twitter API REST . 我正在开发使用Twitter API REST的Web应用程序。 I have a page with Twitter accounts and a button for adding a new account. 我有一个带有Twitter帐户的页面和一个用于添加新帐户的按钮。 When the button is pressed, the function in the Angular controller is executed: 按下按钮后,将执行Angular控制器中的功能:
// Calls token to add a new Twitter user $scope.callToken = function () { $http.get('/token') .then(function (response) { console.log(response); }); };
This is the code in the backend to serve de request. 这是后端中用于处理请求的代码。 It simply redirects to Twitter to add a new user. 它只是重定向到Twitter以添加新用户。
// routes.js var token = express.Router(); token.route('/token') .get(cuentasCtrl.getToken); // Account file exports.getToken = function(req, res) { twitter.getRequestToken(function(err, requestToken, requestSecret) { if (err) res.status(500).send(err); else { _requestSecret = requestSecret; res.setHeader('Access-Control-Allow-Origin', '*'); res.redirect("https://api.twitter.com/oauth/authenticate?oauth_token=" + requestToken); } }); };
But I get the next error: 但是我得到了下一个错误:
Origin 'http://localhost:3000' is therefore not allowed access Twitter API
My server is running on localhost:3000 and If I put localhost:3000/token in my browser there is no problem. 我的服务器在localhost:3000上运行,如果将localhost:3000 / token放在浏览器中,就没有问题。 I have read the solutions for using CORS and tested other browsers but it hasn't worked for me. 我已经阅读了使用CORS的解决方案,并测试了其他浏览器,但是它对我没有用。 What have i done wrong? 我做错了什么?
The redirect you are sending in your express response is caught by the http client you use in the frontend and the redirect happens there. 您在快速响应中发送的重定向被您在前端中使用的http客户端捕获,并且重定向在那里发生。 You are in a classical CORS situation and of course this is not how you can interact with the Twitter rest APIs. 您处于典型的CORS情况,当然,这不是您与Twitter rest API进行交互的方式。
You have to make http calls to Twitter on the node side (server to server) or use a client side client library for Twitter ( https://dev.twitter.com/web/javascript ) 您必须在节点侧(服务器到服务器)对Twitter进行http调用,或者对Twitter使用客户端客户端库( https://dev.twitter.com/web/javascript )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.