首页 > 解决方案 > redirect_uri_mismatch: http://localhost:4500/AUTH/auth/google/callback,不匹配

问题描述

这个问题有很多问题,我都阅读了,但没有看到任何类似的问题。在我的情况下,“auth”是前置的。这是我在应用程序设置中注册为回调 url

http://localhost:4500/auth/google/callback

这是 passport.js 配置:

passport.use(
  new GoogleStrategy.Strategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID!,// "!" is typescript 
      clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
      callbackURL: "auth/google/callback",
    },
    async (accessToken, refreshToken, profile, done) => {
      const existingUser = await User.findOne({ googleId: profile.id });
      if (existingUser) {
        done(undefined, existingUser);
      }
      const user = await new User({ googleId: profile.id }).save();
      done(undefined, user);
    }
  )
);

以下是路线:

export const authRoutes = (app: Application) => {
  //with passing "google" passport knows that it will use GoogleStrategy
  app.get(
    "/auth/google",
    passport.authenticate("google", { scope: ["profile", "email"] })
  );

  app.get("/auth/google/callback", passport.authenticate("google"));
  app.get("/auth/current_user", (req: Request, res: Response) => {
    res.send(req.user);
  });
  app.get("/auth/logout", (req: Request, res: Response) => {
    req.logout();
    res.json({ user: req.user });
  });
};

这是错误消息:

错误 400:redirect_uri_mismatch 请求中的重定向 URI http://localhost:4500/auth/auth/google/callback 与授权给 OAuth 客户端的重定向 URI 不匹配。要更新授权的重定向 URI,请访问:https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

标签: node.jsgoogle-apigoogle-oauthpassport.jsgoogle-api-nodejs-client

解决方案


转到谷歌开发者控制台。确保您正在检查正确的项目和正确的客户。这必须是您在代码中使用的那个。然后加

http://localhost:4500/auth/auth/google/callback

作为重定向 URI。

如果谷歌说它没有设置它没有设置,你需要仔细检查端口和协议是否重要,即使是尾随 / 也会使其匹配失败。


推荐阅读