首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Passport-Google-OAuth回调在Web服务中使用时不起作用

Passport-Google-OAuth回调在Web服务中使用时不起作用
EN

Stack Overflow用户
提问于 2015-07-31 00:46:09
回答 1查看 5.4K关注 0票数 1

我在Node.js web服务项目中使用过Passport-Google-OAuth。我正在使用OAuth2Strategy。

我使用的过程是调用web服务方法来验证Gmail帐户中的用户。最初,我提供的是通过调用Passport-google-OAuth获得的原始HTMl。它工作得很好。

然后,我使用有效的Gmail帐户登录。一旦google调用了回调Url,服务器就会进入无限循环,并在固定的时间间隔后一次又一次地调用回调url。

我对Google的Passport策略配置如下:

代码语言:javascript
复制
    // Use the GoogleStrategy within Passport.
    //   Strategies in Passport require a `verify` function, which accept
    //   credentials (in this case, an accessToken, refreshToken, and Google
    //   profile), and invoke a callback with a user object.
    passport.use(new GoogleStrategy({
            clientID        : "948630708036-2t6mestiv81gtv0s9n6iptoava4o1cpa.apps.googleusercontent.com",
            clientSecret    : "omugRnr7nad2yMmefiZdBaLL",
            callbackURL     : "http://localhost:4000/api/auth/google/callback"
        },
        function(token, refreshToken, profile, done) {
            console.log('Inside global callback.');
            // make the code asynchronous
            // User.findOne won't fire until we have all our data back from Google
            process.nextTick(function() {

                // try to find the user based on their google id
                User.findOne({ 'google.id' : profile.id }, function(err, user) {
                    if (err)
                        return done(err);

                    if (user) {

                        // if a user is found, log them in
                        return done(null, user);
                        
                    } else {
                        // if the user isnt in our database, create a new user
                        var newUser          = new User();

                        // set all of the relevant information
                        newUser.google.id    = profile.id;
                        newUser.google.token = token;
                        newUser.google.name  = profile.displayName;
                        newUser.google.email = profile.emails[0].value; // pull the first email

                        return done(null, newUser);
                       
                    }
                });
            });
        }));

然后,我从服务项目中的端点调用Passport:

代码语言:javascript
复制
passport.authenticate('google', { session:false,scope : ['profile', 'email'] });

回调URL包含以下代码,我将在其中以JSON格式将返回的用户Google帐户详细信息发送到最初访问web服务的客户端。

代码语言:javascript
复制
function(req, res) {
  console.log('Callback by Google:'+res.body+' || '+ res.headers);
  console.log('Response Object:'+util.inspect(res));
  passport.authenticate('google', {	session : false	}),function(req,res){
					console.log('Callback authenticated.User: +req.user);
                    res.json(req.user);
            }

在日志中,我得到了"Callback by Google: undefined || undefined“。

我将禁用会话,因为这将是API服务器向各种客户端提供数据。

我不知道我做错了什么。请指出在API(网络服务)服务器中使用Passport-Google-OAuth(OAuth2Strategy)的任何资源或示例。我是不是需要走别的路。提前感谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2015-08-01 21:40:30

您的路由可能有问题。请看这里的教程

https://scotch.io/tutorials/easy-node-authentication-google

这是我见过的最好的。我也实现了类似的东西。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31729592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档