2016-07-08 2 views
6

노드 js의 여권 모듈을 사용하여 Google 버튼으로 로그인하려고합니다. 나는 사람의 이메일 ID, 이름, 프로필 사진을 얻으려고 노력하고있다. 로컬 서버에 그림을 다운로드하려고합니다. Google은 범위에 '이메일'을 추가 한 후에도 이메일 ID를 반환하지 않으며 프로필 사진의 반환 링크도 작동하지 않습니다. 나는이 질문에 대한 다양한 해답을 살펴 보았지만, 모두 userinfo.email을 포함 시키라는 말은 이제는 더 이상 사용되지 않을 것이다. 구글 문서에 따라 새 범위 매개 변수가 이메일 것은 아래에 어떤 도움이 여권Google 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

위의 대답은 분명히 작동합니다.이 문제에 대해 한 가지 더 많은 방법이 있습니다. profileroutes.js에서

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

email를 추가합니다.

이렇게하면 문제가 해결됩니다.

관련 문제