簡體   English   中英

在heroku中部署時,Google回調網址會返回400

[英]Google callback url giving back 400 when deployed in heroku

我使用 MEAN 框架開發了一個應用程序,並使用了passportjs 的谷歌策略進行身份驗證。 本地運行運行良好,但是當我將其部署到 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,則 localhost 部分工作正常。 但是,當我嘗試對 heroku 應用程序執行相同操作時,出現 400 服務器錯誤,錯誤為 Error: redirect_uri_mismatch。

我該如何解決這個問題? 我非常接近部署這個應用程序並堅持使用這個東西。 如果您需要更多信息,請告訴我。

您需要添加 heroku 域名和 heroku 回調 url,請參見下面的工作示例:

在此處輸入圖片說明

您可以在 callbackURL 處使用相對路徑(例如 callbackURL: '/auth/google/callback'),使您的代碼不那么依賴於特定域,並在 googleAuth 上添加一個附加元素,方法是在 callbackURL 后放置一個逗號,如下所示:

proxy: true

在此之后,您可以在 google 的開發人員控制台上使用 https 進行回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM