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

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

问题描述

我在 Node.js 网络服务项目中使用过 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 收到的原始 HTMl.哪个工作正常.

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 帐户登录.一旦回调 URL 被谷歌调用,服务器就会进入无限循环,并在固定的时间间隔后一次又一次地调用回调 URL.

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 包含以下代码,我将返回的用户的 Google 帐户详细信息以 JSON 格式发送到最初访问 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 回调:未定义 || 未定义".

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(Web 服务)服务器中使用 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.

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

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