5

我的 DNS 将两个主机名发送到同一个 IP:

  • 方舟信息
  • www.theark.info

我在我的 Twitter 应用程序中使用theark.info. 什么是确保我可以使用相同的 Twitter 应用程序进行 Oauth 的最佳方法www.theark.info,因为我目前收到一个错误:

内部服务器错误

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

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

4

1 回答 1

2

您不能更改 Twitter(或任何其他 OAuth 提供商),它们都只提供一个回调到一个域。一个简单的解决方案是将所有请求从http://domain.com重新路由到http://www.domain.com,这样所有访问者都会在验证之前访问 www.domain.com。您应该能够在您的 DNS 上或使用 req.header 重定向服务器端执行此操作:

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 进行身份验证时,尝试指定回调 url:

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 中配置的完全相同。如果您在 localhost 上运行 node 进行开发,请尝试两个不同的密钥文件并在 twitter 上创建另一个身份验证应用程序,并将 127.0.0.1:3000 作为回调地址。您可以切换开发和生产的关键文件:

if (app.get('env') == 'development') {
  auth_keys = require('./lib/keys_dev');
} 
if (app.get('env') == 'production') {
  auth_keys = require('./lib/keys_live');
}
于 2012-08-23T07:24:51.580 回答