2016-07-08 38 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

這應該可以解決您的問題。