如何Twitter使用相同的Twitter应用程序从一个域的多个主机名进行身份validation?

我有两个主机名,通过我的DNS发送到同一个IP:

  • theark.info
  • www.theark.info

我使用theark.info在我的Twitter应用程序中明确地设置了我的callback和域名。 什么是最好的方式,以确保我可以使用相同的Twitter应用程序Oauth使用www.theark.info ,因为我目前得到一个错误:

内部服务器错误

在我的DNS中,我的DNS中有一个CNAME www ,指向theark.info

也许我需要使用Express和Javacsript在请求上操作DOM?

您不能更改Twitter(或任何其他OAuth提供商),他们都只提供一个域的callback。 一个简单的解决scheme是将所有来自http://domain.com的请求重新路由到http://www.domain.com ,所有访问者在进行身份validation之前都会访问www.domain.com。 你应该可以在你的DNS或req.headerredirect服务器端执行此操作:

 app.get('/*', function(req, res, next) { if (req.headers.host.match(/^www/) !== null ) { res.redirect('http://' + req.headers.host.replace(/^www\./, '') + req.url); } else { next(); } }) 

从这个答案复制。

使用passport.js进行身份validation时,请尝试指定callbackurl:

 passport.use(new TwitterStrategy({ consumerKey: auth_keys.twitter.consumerKey, consumerSecret: auth_keys.twitter.consumerSecret, callbackURL: auth_keys.twitter.callbackURL }, function(token, tokenSecret, profile, done) { process.nextTick(function () { User.twitterAuth({ profile: profile }, function (err, user) { return done(err, user); }); }); } )); 

并确保callbackURL与Twitter中configuration的完全相同。 如果您正在本地主机上运行节点进行开发,请尝试使用两个不同的密钥文件,并使用127.0.0.1:3000作为callback地址在Twitter上创build另一个身份validation应用程序。 您可以切换开发和生产的关键文件:

 if (app.get('env') == 'development') { auth_keys = require('./lib/keys_dev'); } if (app.get('env') == 'production') { auth_keys = require('./lib/keys_live'); }