node.js - 在 Heroku 中部署时,Google 回调 url 返回 400

标签 node.js heroku passport.js mean-stack google-oauth

我使用MEAN框架开发了一个应用程序,并使用passportjs的google策略进行身份验证。本地运行运行良好,但当我将其部署到 Heroku 时,因为 Heroku 在随机端口上运行其应用程序。我不确定需要在 Google 控制台的“授权重定向 URI”中添加什么 Google 回调 URL。

passport.use(new GoogleStrategy({
    clientID: config.googleAuth.clientID,
    clientSecret: config.googleAuth.clientSecret,
    callbackURL: config.googleAuth.callbackURL
}, function (token, refreshToken, profile, done) {

    console.log(token, refreshToken, profile, done);
    var query = {
        'google.id' : profile.id
    };
    User.findOne(query, function (err, user) {
        if(user){
            console.log("User found in the database");
            done(null, user);
        }
        else{
            var newUser = new User;
            newUser.displayName = profile.displayName;
            newUser.image = profile.photos[0].value;
            newUser.google = {};
            newUser.google.id = profile.id;
            newUser.google.token = token;
            newUser.save();
            console.log("saved user to the database");
            done(null, newUser);
        }
    });
}));

上面显示的代码是我的谷歌策略。我正在使用 Passport-google-oauth lib 进行身份验证。

    module.exports = {
    development: {
        rootPath: rootPath,
        db: 'xxx',
        port: process.env.PORT || 3030,
        googleAuth: {
          clientID: 'xxx',
          clientSecret: 'xxx',
          callbackURL: 'http://localhost:3030/auth/google/callback'
        }
      },
      production: {
        rootPath: rootPath,
        db: 'xxx',
        port: process.env.PORT || 80,
        googleAuth: {
          clientID: 'xxx',
          clientSecret: 'xxxx',
          callbackURL: 'https://<myheroku-app>:<heroku-port-no>/auth/google/callback'
        }
      }
}

以上是我的Google策略的详细内容。如果我添加 http://localhost:3030/auth/google/callback 本地主机部分工作正常到授权重定向 URI。但是,当我尝试对 heroku 应用程序执行相同操作时,我收到 400 服务器错误,错误为 Error:redirect_uri_mismatch。

我该如何解决这个问题?我非常接近部署这个应用程序并且坚持使用这个东西。如果您需要更多信息,请告诉我。

最佳答案

您需要添加heroku域名和heroku回调url,请参阅下面的工作示例:

enter image description here

关于node.js - 在 Heroku 中部署时,Google 回调 url 返回 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42919489/

相关文章:

node.js - 哦哦 好像出问题了。请稍后再试。雅虎 Passport

node.js - 如何阅读使用 Node 在Firestore中创建的最新文档?

node.js - Webpack 中的 Typescript 定义文件

ruby-on-rails - Heroku Assets 预编译问题

heroku - 有没有一种方法可以使用 --bigkeys 等选项运行 heroku redis-cli?

mysql - session 管理mysql和nodejs

node.js - 如何通过.authenticate将变量传递给 Passport 策略?

node.js - 为 MEAN 应用程序生成 Mongoose DTO

ruby-on-rails - Rails 4、omniauth-facebook、rack-cors、Heroku 的 CORS 问题

passport.js - Passport - Azure AD - 此 API 版本不支持应用程序