简体   繁体   English

在heroku中部署时,Google回调网址会返回400

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

I developed an app using MEAN framework and used passportjs's google strategy to authenticate.我使用 MEAN 框架开发了一个应用程序,并使用了passportjs 的谷歌策略进行身份验证。 The local run runs fine, but when I deploy the same to heroku, since Heroku runs its app on a random port.本地运行运行良好,但是当我将其部署到 heroku 时,因为 Heroku 在随机端口上运行其应用程序。 I am not sure what google callback url I need to add in my google console's "Authorized redirect URIs".我不确定需要在我的 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);
        }
    });
}));

The above shown code is my google strategy.上面显示的代码是我的谷歌策略。 I am using passport-google-oauth lib for authentication.我正在使用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'
        }
      }
}

The above is the details of my google strategy.以上就是我的google策略的详细介绍。 The localhost part works fine if I add http://localhost:3030/auth/google/callback to the Authorized redirect URI's.如果我将http://localhost:3030/auth/google/callback 添加到授权重定向 URI,则 localhost 部分工作正常。 But when I try to do the same for the heroku app, I get a 400 server error with Error: redirect_uri_mismatch as the error.但是,当我尝试对 heroku 应用程序执行相同操作时,出现 400 服务器错误,错误为 Error: redirect_uri_mismatch。

How do I fix this thing?我该如何解决这个问题? I am so close to deploying this app and stuck with just this thing.我非常接近部署这个应用程序并坚持使用这个东西。 Let me know if you would need any more info.如果您需要更多信息,请告诉我。

You need to add heroku domain name and heroku callback url see working example below:您需要添加 heroku 域名和 heroku 回调 url,请参见下面的工作示例:

在此处输入图片说明

You could use relative paths at callbackURL (eg callbackURL: '/auth/google/callback') to make your code less depended on specific domains, and add an additional element on googleAuth, by putting a comma after callbackURL, as:您可以在 callbackURL 处使用相对路径(例如 callbackURL: '/auth/google/callback'),使您的代码不那么依赖于特定域,并在 googleAuth 上添加一个附加元素,方法是在 callbackURL 后放置一个逗号,如下所示:

proxy: true

After this, you can use https on google's developers console for your callbacks.在此之后,您可以在 google 的开发人员控制台上使用 https 进行回调。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署到 Heroku 时无法使用 Google OAuth 登录 - Unable to login with Google OAuth when deployed to Heroku 我正在尝试使用 heroku 部署一个简单的 nodejs 应用程序,它已成功部署,但在打开 url 时出错? - I am trying to deploy a simple nodejs app using heroku and its is deployed successfully but giving error on opening the url? 在Heroku上获取Google OAuth2回调时的循环结构 - Circular structure when getting Google OAuth2 callback on Heroku heroku:部署时应用程序未显示 - heroku: application not showing when deployed 在Heroku上部署RXjs问题 - RXjs issues when deployed on Heroku 部署在 heroku 上的节点应用程序 - 出现 404 错误(即使 url 是正确的) - Node app deployed on heroku - getting 404 error (even when url is correct) 我的节点应用程序在 localhost 上运行良好,但是当我部署在 heroku 上时,它给了我应用程序错误。 你可以阅读下面的描述 - my node app is working fine on localhost but when i deployed on heroku , it is giving me application error. you can read description below 在 Heroku 上实时部署时,passport google oauth2 会导致“内部服务器错误” - passport google oauth2 causes “internal server error” when deployed live on Heroku AngularJS +节点:部署到Heroku时为空白页 - AngularJS + Node: Blank page when deployed to Heroku 节点程序包在本地工作,但在部署到Heroku时不工作 - Node packages work locally but not when deployed to Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM