2016-07-08 17 views
6

Ich versuche, eine Anmeldung mit Google-Taste mit Pass-Modul von Knoten js. Ich versuche, Personen E-Mail-ID, Name, Profilbild zu bekommen. Ich versuche, Bild auf lokalen Server herunterzuladen. Google gibt die E-Mail-ID nicht zurück, selbst nachdem der Bereich "E-Mail" hinzugefügt wurde, und auch der zurückgegebene Link für das Profilbild funktioniert nicht. Ich habe verschiedene Antworten auf diese Frage untersucht, aber alle sagen, dass sie userinfo.email einschließen. Gemäß Google-Dokumentation neuer Bereichsparameter ist E-Mail unten ist mein Code Jede Hilfe PassGoogle Oauth nicht zurück E-Mail-Pass-Authentifizierung

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

Antwort

6

hatte geschätzt I das gleiche Problem und schrieb den Umfang auf diese Weise:

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

Und Sie werden die E-Mail erhalten:

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

Ich hoffe, das hilft dir.

+0

Vielen Dank. Ich habe ich arbeiten :) können Sie bitte helfen Sie mir in fb oauth auch. Ich habe es funktioniert, aber Tokenization Fehler bekommen. Hier ist der Link http://stackoverflow.com/questions/38299165/how-to-use-random-guid-with-passport-module-and-express-server –

+0

Haben Sie erhalten Autorisierung Bildschirm jedes Mal, wenn API versucht, Benutzerprofil zuzugreifen ? –

+0

Die Wahrheit ist, dass ich damit beginne. Ich muss mehr Tests machen, aber im Moment habe ich dieses Problem nicht. Der Login mit Facebook habe ich nicht implementiert. Wenn ich daran arbeite, werde ich versuchen, Ihnen bei Ihrer Frage zu helfen. – pariasdev

2

Die obige Antwort funktioniert definitiv, es gibt auch eine weitere Möglichkeit, dies zu erreichen.

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

In Ihrem routes.js mit dem profileemail hinzuzufügen.

Dies sollte Ihr Problem lösen.