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

Shu*_*ham 12 node.js express google-oauth passport.js

我正在尝试使用节点js的护照模块使用谷歌按钮登录.我想让人们发送电子邮件ID,姓名,个人资料照片.我正在尝试将pic下载到本地服务器.即使在向作用域添加"电子邮件"之后,Google也没有返回电子邮件ID,并且返回的个人资料照片链接也无效.我已经查看了这个问题的各种答案,但是所有人都说要包含userinfo.email.现在已经弃用了.根据谷歌文档新的范围参数是下面的电子邮件是我的代码任何帮助是赞赏护照

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

}));
};
Run Code Online (Sandbox Code Playgroud)

routes.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 : '/'
        }));
Run Code Online (Sandbox Code Playgroud)

download.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!");
});
Run Code Online (Sandbox Code Playgroud)

小智 20

我遇到了同样的问题并以这种方式编写了范围:

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

你会收到电子邮件:

function(accessToken, refreshToken, profile, done) {
    console.log(profile.emails[0].value);
}); 
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你.


Dee*_*ndi 11

上面的答案肯定有效,还有一种方法可以解决这个问题.

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

在你routes.jsprofile添加email.

这应该可以解决您的问题.


小智 6

根据 oauth 的 google 文档,第一个参数必须是 openid,第二个参数可以是电子邮件或个人资料,或两者兼而有之

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

文档