1

我在论坛上多次看到这个问题,但使用这些解决方案并没有帮助。

我正在使用 PassportJS 构建 MEAN 堆栈应用程序以允许使用 Twitter 登录。

angular.module('HomeCtrl', []).controller('HomeController', function($scope,$http) {

$scope.tagline = 'To the moon and back!';   
$scope.twit = function() {
    console.log("twitter button clicked");
    $http.get("/auth/twitter")
    .success(function (data) {
        console.log(data);
    })
    //$window.location.href = '/auth/twitter';
}});

我在服务器中的 route.js 有

app.get('/auth/twitter', passport.authenticate('twitter', { scope : 'email' }));

现在,当 twitter 重定向到应用程序时,由于 CORS 问题,服务器重定向不起作用,我能够理解。要解决这个问题

我试过以下

app.all('/*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:8080");
   res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
  next();
});

但这仍然行不通。在网上阅读更多内容后,我的理解是 twitter 不发送 header-origin ,并且由于它是一个重定向,所以 node.js 服务器无法控制它收到的可以发送回浏览器的响应。

在这个阶段,我不确定如何进行。请指教

根据评论:这是 twitter 回调的响应

Request URL:https://api.twitter.com/oauth/authenticate?oauth_token=l8OKcAvqr3QLrlCroweGgByvvhXfSmIiqhvRgGqML6c
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Query String Parametersview sourceview URL encoded
oauth_token:l8OKcAvqr3QLrlCroweGgByvvhXfSmIiqhvRgGqML6c
4

1 回答 1

-1

这里的问题是 twitter 没有设置 CORS 标头(不是您自己的节点服务器)。你无法解决这个问题。因此,您可以使用简单的墨迹<a href="/auth/twitter"></a>代替 $http.get 或 a $window.location.href. 请注意,在这种情况下您不会应用 http 拦截器,因此您可能需要传递一些查询参数。

于 2016-01-15T19:56:49.410 回答