PassportJs Google OAuth2回调引发无法访问该网站的错误

时间:2019-02-12 07:45:51

标签: node.js oauth-2.0 passport.js

我正在尝试在个人项目中设置“使用Google登录”,当我调用google auth路由时,在回调中收到“无法访问此网站的错误”。

我的路线:

    app.get(
  "/user/auth/google",
  passport.authenticate("google", {
    scope: [
      "https://www.googleapis.com/auth/userinfo.profile",
      "https://www.googleapis.com/auth/userinfo.email"
    ]
  })
);

app.get(
  "/user/auth/google/callback",
  passport.authenticate("google", {
    successRedirect: "/wsup",
    failureRedirect: "/login"
  }),
  (req, res) => {
    console.log("callback called");
  }
);

我的Google策略

passport.use(
  new googleStrategy(
    {
      clientID: process.env.googleClientID,
      clientSecret: process.env.googleClientSecret,
      callbackURL: "https://localhost:3000/user/auth/google/callback",
      proxy: true
    },
    (accessToken, refreshToken, profile, done) => {
      console.log(profile.emails[0].value);
      User.findOne({ googleId: profile.id }).then(user => {
        if (user) {
          console.log("existing");
          done(null, user);
        } else {
          new User({ googleId: profile.id })
            .save()
            .then(newUser => done(null, newUser));
        }
      });
    }
  )
);

回复文字:

This site can’t be reached. localhost unexpectedly closed the connection.

我想补充一点,我正在localhost上对其进行测试,并且可以在回调URl中看到来自Google的代码

1 个答案:

答案 0 :(得分:0)

我想通了。回调程序试图访问https://localhost,我要做的就是在Google Api控制台中将https更改为http,它的工作原理很像