简体   繁体   English

Google oauth 不返回电子邮件护照身份验证

[英]Google oauth not returning email passport authentication

I am trying to make a sign in with google button using passport module of node js.我正在尝试使用节点 js 的护照模块使用谷歌按钮登录。 I am trying to get person's email id, name, profile pic.我正在尝试获取某人的电子邮件 ID、姓名、个人资料图片。 I am trying to download pic to local server.我正在尝试将图片下载到本地服务器。 Google is not returning email id even after adding 'email' to scope and nor the returned link for profile pic is working.即使将“电子邮件”添加到范围后,Google 也不会返回电子邮件 ID,并且返回的个人资料图片链接也不起作用。 I have looked into various answers to this question but all say to include userinfo.email.我研究了这个问题的各种答案,但都说要包括 userinfo.email。 It has been deprecated now.它现在已被弃用。 As per google documentation new scope parameter is email.根据谷歌文档,新范围参数是电子邮件。

Below is my code.下面是我的代码。 Any help is appreciated.任何帮助表示赞赏。

Passport护照

passport.use(new GoogleStrategy({

    clientID        : configAuth.googleAuth.clientID,
    clientSecret    : configAuth.googleAuth.clientSecret,
    callbackURL     : configAuth.googleAuth.callbackURL,
},
function(token, refreshToken, profile, done) {

    // 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();
                console.log(profile);
                //JSON.parse(profile);
                // set all of the relevant information
                newUser.google.id    = profile.id;
                newUser.google.token = profile.token;
                newUser.google.name  = profile.displayName;
                newUser.google.uname = profile.emails[0].value; // pull the first email
                newUser.google.dp    = profile._json.picture;
                console.log('url is');
                console.log(newUser.google.name);
                console.log(newUser.google.dp);
                //console.log(profile.picture);
                Download(newUser.google.uname, newUser.google.dp,function(err){
                    if(err)
                        console.log('error in dp');
                    else
                        console.log('Profile Picture downloaded');
                });

                // save the user
                newUser.save(function(err) {
                    if (err)
                        throw err;
                    return done(null, newUser);
                });
            }
        });
    });

}));
};

routes.js路由.js

    app.get('/connect/google', passport.authorize('google', { scope : ['profile', 'email'] }));

    // the callback after google has authorized the user
    app.get('/connect/google/callback',
        passport.authorize('google', {
            successRedirect : '/profile',
            failureRedirect : '/'
        }));

download.js下载.js

    module.exports = function(username, uri, callback){
var destination;

request(uri).pipe(fs.createWriteStream("./downloads/"+username+".png"))
.on('close', function(){
    console.log("saving process is done!");
});

I had the same problem and wrote the scope in this way:我遇到了同样的问题,并以这种方式编写了范围:

app.get('/connect/google', passport.authenticate('google', {
    scope: [
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email'
    ]
}));

And you will get the email:您将收到以下电子邮件:

function(accessToken, refreshToken, profile, done) {
    console.log(profile.emails[0].value);
}); 

I hope this helps you.我希望这对你有帮助。

The above answer definitely works, there is also one more way to approach this.上面的答案肯定有效,还有另一种方法可以解决这个问题。

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

In your routes.js with the profile add email .在带有profileroutes.js添加email

This should resolve your issue.这应该可以解决您的问题。

According to google documentation for oauth, the first parameter has to be openid and the second can be email or profile, or both根据 oauth 的谷歌文档,第一个参数必须是 openid,第二个参数可以是电子邮件或个人资料,或两者兼而有之

app.get('/auth/google',
    passport.authenticate('google', {scope: ['openid', 'email', 'profile']})
);

documentation 文件

在 callbackUrl 之后添加代码行。

userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM