【问题标题】:Google callback url giving back 400 when deployed in heroku在 Heroku 中部署时,Google 回调 url 返回 400
【发布时间】:2017-08-12 16:03:22
【问题描述】:

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

    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,则 localhost 部分可以正常工作。但是当我尝试对 heroku 应用程序执行相同操作时,我收到 400 服务器错误,错误为:redirect_uri_mismatch。

我该如何解决这个问题?我非常接近部署这个应用程序并坚持使用这个东西。让我知道您是否需要更多信息。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • Aaah....gotcha...默认情况下,heroku 给了我 https url。我所要做的就是将其更改为http。非常感谢!
    【解决方案2】:

    您可以在 callbackURL 处使用相对路径(例如 callbackURL: '/auth/google/callback'),以减少您的代码对特定域的依赖,并在 googleAuth 上添加一个附加元素,方法是在 callbackURL 后添加一个逗号,如下所示:

    proxy: true
    

    之后,您可以在 Google 的开发者控制台上使用 https 进行回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2012-06-28
      相关资源
      最近更新 更多