在Web服务中使用Passport-Google-OAuth回调时不起作用 [英] Passport-Google-OAuth Callback Not working when used in Web Service

查看:91
本文介绍了在Web服务中使用Passport-Google-OAuth回调时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在Node.js Web服务项目中使用Passport-Google-OAuth.我正在使用OAuth2Strategy.

I Have used Passport-Google-OAuth in Node.js web service project. I am using OAuth2Strategy.

我使用的过程是我调用Web服务方法来从用户的Gmail帐户进行身份验证.最初,我是通过致电Passport-google-OAuth收到的原始HTM1服务的.效果很好.

The process i have used is i call the web service method to authenticate user from his Gmail account. Initially i serve the Raw HTMl which i receive from calling the Passport-google-OAuth. Which works fine.

然后,我使用有效的Gmail帐户登录.一旦Google调用了回调网址,服务器就会进入无限循环,并在固定的时间间隔后再次调用回调网址.

Then i login with valid Gmail accounts. Once the Callback Url is called by google the server goes into infinite loop and calls the callback url again and again after fixed interval of time.

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

My Passport strategy configuration for Google is like this:

    // 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:

Then i am calling the Passport from the endpoint in the service project:

passport.authenticate('google', { session:false,scope : ['profile', 'email'] });

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

And the Callback URL contains the following code where i am sending the returned Google account details of the user in JSON format to the client which accessed the web service intially.

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);
            }

在日志中,我收到"Google的回叫:undefined || undefined".

In the Log i am getting "Callback by Google: undefined || undefined".

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

I am disabling sessions since this will be the API Server feeding data to various clients.

我不知道我在做什么错.请指出在API(网络服务)服务器中使用Passport-Google-OAuth(OAuth2Strategy)的任何资源或示例.我需要遵循其他方式吗?感谢您的提前帮助.

I dont know what mistake i am doing. Kindly point out any resource or example where the Passport-Google-OAuth(OAuth2Strategy) is used in a API(Web Service) server. Do i need to follow some other way. Thanks for ur help in advance.

推荐答案

您的路线可能有问题.在这里查看教程

There may be a problem in your routes. Look at the tutorial here

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

这是我所见过的最好的.而且我已经实现了类似的方法.

It's the best I have seen. And I have implemented something similar.

这篇关于在Web服务中使用Passport-Google-OAuth回调时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆