5

angular.js の $http モジュールを使用して Twitter アプリを承認しようとすると、次のようになります。

XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=something. Origin null is not allowed by Access-Control-Allow-Origin. 

クライアントコード:

$http({
   method: 'GET',
   headers: { "Content-Type": undefined },
   url: '/oauth/twitter'
});

サーバーコード:

app.configure(function () {
   app.use(express.cookieParser());
   app.use(express.cookieSession({ secret: 'tobo!', cookie: { maxAge: 3600 }}));

   app.use(express.session({secret: 'secret'}));
   app.use(passport.initialize());
   app.use(passport.session());
});

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

app.get('/oauth/twitter', passport.authenticate('twitter'), function (req, res) {
// The request will be redirected to Twitter for authentication, so this
// function will not be called.
console.log('ouath/twitter')
});

app.get('/oauth/twitter/callback', passport.authenticate('twitter', { successRedirect:  '/', failureRedirect: '/login' }));

ただし、oauth/twitter アドレスのハイパーリンクを使用すると問題なく動作します。何が問題なのかわからない。ほとんどの人はオリジンは許可されていないと言いましたが、「*」はすべてのアドレスがサーバーに接続できるようにする必要があります。

4

3 に答える 3

3

私は同じ問題を抱えていましたが、私が見つけた唯一の解決策は、ハイパーリンクを使用することです (投稿で述べたように)。セキュリティ上の理由から ajax リクエストは使用できないと思います。ただし、ハイパーリンクを使用するとうまくいき、ユーザーを承認できます。

ハイパーリンクを使用しないのはなぜですか? 何か問題がありますか?

于 2014-01-20T10:38:10.380 に答える