2016-07-08 29 views
6

Sto tentando di accedere con il pulsante google utilizzando il modulo passaporto del nodo js. Sto cercando di ottenere le e-mail delle persone, il nome, la foto del profilo. Sto cercando di scaricare le immagini sul server locale. Google non restituisce l'ID della posta elettronica anche dopo aver aggiunto "email" all'ambito e né il link restituito per la foto del profilo funziona. Ho esaminato varie risposte a questa domanda, ma tutto dice di includere userinfo.email. Ora è stato deprecato. Come da documentazione google nuovo parametro campo di applicazione è posta sotto è il mio codice Ogni aiuto è apprezzato PassaportoGoogle oauth non restituisce l'autenticazione del passaporto e-mail

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

risposta

6

ho avuto lo stesso problema e ha scritto l'ambito in questo modo:

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

E si otterrà l'e-mail:

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

Spero che questo ti aiuta.

+0

Grazie. Ho lavorato io :) puoi aiutarmi anche in fb oauth. Ho funzionato, ma ho ricevuto un errore di tokenizzazione. Ecco il link http://stackoverflow.com/questions/38299165/how-to-use-random-guid-with-passport-module-and-express-server –

+0

hai ricevuto la schermata di autorizzazione ogni volta che api prova ad accedere al profilo utente ? –

+0

La verità è che sto iniziando con questo. Devo fare più test, ma per ora non ho questo problema. Il login con Facebook non ho implementato. Quando lavoro su questo, se posso, cercherò di aiutarti con la tua domanda. – pariasdev

2

La risposta sopra sicuramente funziona, c'è anche un altro modo per avvicinarsi a questo.

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

Nel vostro routes.js con la profile aggiungere email.

Questo dovrebbe risolvere il problema.

Problemi correlati