2016-07-08 42 views
6

我想用谷歌按钮使用护照模块的节点js登录。我试图让人员电子邮件ID,姓名,个人资料图片。我正试图将图片下载到本地服务器。即使将“电子邮件”添加到范围内,Google也没有返回电子邮件ID,也没有返回的个人资料照片链接正在工作。我已经查看了这个问题的各种答案,但都说包含userinfo.email。it现在已被弃用。按照谷歌文档的新范围参数是电子邮件 下面是我的代码 任何帮助表示赞赏 护照谷歌oauth没有返回电子邮件护照认证

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

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

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

回答

6

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

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

,你会得到电子邮件:

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

我希望这可以帮助你。

+0

谢谢。我得到了我的工作:) 你能帮我在FB OAUTH也。我已经得到它的工作,但得到令牌化错误。这里是链接http://stackoverflow.com/questions/38299165/how-to-use-random-guid-with-passport-module-and-express-server –

+0

你有没有获得授权屏幕每次API尝试访问用户配置文件? –

+0

事实是,我从这开始。我必须做更多的测试,但现在没有这个问题。 用Facebook登录我还没有实现。当我研究这个问题时,如果可以的话,我会尽力帮助你解决你的问题。 – pariasdev

2

上面的答案绝对有效,还有一种方法可以解决这个问题。

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

在你routes.jsprofile添加email

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