繁体   English   中英

OAuth2 - 错误 400:redirect_uri_mismatch 与 Passport,重定向 URL 与设置的不同

[英]OAuth2 - Error 400 : redirect_uri_mismatch with Passport, Redirect URL is different than what was set

我尝试使用护照对我的快递 API 实施 google OAuth2,但出现错误:

在此处输入图像描述

---------------------------------------------
users.js路由文件:

...
const GoogleStrategy = require('passport-google-oauth20').Strategy
...

passport.use(new GoogleStrategy(
    {
        clientID: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        callbackURL: 'users/auth/google/callback'
    }, accessToken => {
        console.log(accessToken);
    }
    )
)
...
router.get('/auth/google', passport.authenticate('google', {scope: ['profile', 'email']}))
...

在 Google 开发人员控制台中,我将授权重定向 URI 设置为:

http://localhost:3000/users/auth/google/callback 我的客户端 ID 和密码是正确的。

在错误消息中,我可以看到重定向 URI 是:http://localhost:3000/auth/users/auth/google/callback

Url路径的开头还有一个额外的auth ,这很奇怪,因为我没有设置它。 我怎样才能删除它? 我什至可以删除它吗?

首先,您将 url 回调到“users/auth/google/callback”,因此您必须创建它:

router.get('users/auth/google/callback', 
  passport.authenticate('google', { failureRedirect: '/login' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM