使用Passport-google-oauth20

时间:2018-06-23 20:16:44

标签: node.js oauth-2.0 google-oauth2

我刚刚开始学习OAuth。我正在将node-js与passport-google-oauth20一起使用。下面是我项目的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)给我一个错误/消息,如屏幕截图所示。enter image description here

在Firefox上,消息显示如下(仅由于错误消息有所不同而添加)

enter image description here

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

enter image description here

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

enter image description here

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

1 个答案:

答案 0 :(得分:1)

该过程不完整。现在,我已经完成了我的项目,并为此写了一个答案。请从这里继续关注我对Google Oauth giving code redeemed error

的回答