首页 > 解决方案 > 使用 passport-google-oauth20 时无法访问重定向 URL

问题描述

我刚开始学习 OAuth。我正在将passport-google-oauth20 与node.js 一起使用。下面是我项目的 passport-setup.js 文件中的代码。

const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const keys = require('./keys')

passport.use(
    new GoogleStrategy({
        //options for google strategy
        callbackURL:'https://localhost:3000/auth/google/redirect',
        clientID:keys.google.clientID,
        clientSecret:keys.google.clientSecret
    }, function(){
        //passport callback function
})
);

我项目的 auth-route.js 文件中的代码

const router = require('express').Router();
const passport = require('passport');

//auth login
router.get('/login', function(req,res){
    res.render('login');
});

//auth logout
router.get('/logout', function(req,res){
    //have to handle with passport
    res.send('Logging out');
});

//auth with google
router.get('/google', passport.authenticate('google',{
    scope:['profile']
}));

//callback route for google
router.get('/google/redirect', passport.authenticate('google'), 
    function(req,res){
        res.send('You logged in');
});

module.exports = router;

可以参考完整代码@https ://github.com/Ayush-Porwal/OAuth-learning.git

我对重定向 URL 有问题。当 callbackURL 持有“ https://localhost:3000/auth/google/redirect ”的值时,浏览器会将我定向到 OAuth 同意屏幕。但是,当我尝试登录后,浏览器(chrome)会给我一个错误/消息,如屏幕截图所示。在此处输入图像描述

而在 Firefox 上,消息如图所示(添加只是因为错误消息不同)

在此处输入图像描述

当 callbackURL 保持“/auth/google/redirect”的值时(我认为这两个值应该给出相同的结果)。浏览器给出的错误为

在此处输入图像描述

这是我在谷歌开发者网站上的应用程序中输入的 URI 的屏幕截图。

在此处输入图像描述

我尝试了几件事,例如稍微更改代码等,但我无法意识到问题出在哪里。请帮忙。

标签: node.jsoauth-2.0google-oauth

解决方案


过程不完整。我现在已经完成了我的项目,并且我已经写了一个相同的答案。请从这里跟进我在Google Oauth 上给出代码兑换错误的答案


推荐阅读