【问题标题】:redirect_uri_mismatch: http://localhost:4500/AUTH/auth/google/callback, does not match tredirect_uri_mismatch: http://localhost:4500/AUTH/auth/google/callback,不匹配
【发布时间】:2021-03-05 18:18:02
【问题描述】:

这个问题有很多问题,我都阅读了,但没有看到任何类似的问题。在我的情况下,“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,请访问:https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

【问题讨论】:

  • 你能添加整个错误信息吗? (标题只有一半的错误信息,没用)
  • @LawrenceCherone 没有感叹号。我没有将回调 url 设置为 env。只有数据库、客户端 ID 和客户端密码
  • "localhost:4500/auth/auth/google/callback" 为什么这个有 2 个auth 部分?
  • @LawrenceCherone 这是打字稿。抱歉,我应该提到这一点。我会更新问题
  • @Dilshan 这就是问题所在。我不明白谷歌会返回“/auth/auth/google/callback”

标签: node.js google-api google-oauth passport.js google-api-nodejs-client


【解决方案1】:

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

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

作为重定向 URI。

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

【讨论】:

  • DalmTo,感谢您的回复。我不明白为什么以及如何获得此网址“localhost:4500/auth/auth/google/callback
  • 您正在使用 Google apis 节点 js 客户端库,这是它旨在使用的重定向 uri。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 2017-06-01
  • 2018-05-18
  • 1970-01-01
相关资源
最近更新 更多