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