2016-07-08 3 views
6

ノードjsのパスポートモジュールを使用してgoogleボタンでサインインしようとしています。私は人のメールID、名前、プロフィール写真を取得しようとしています。私はローカルサーバーに写真をダウンロードしようとしています。スコープに「電子メール」を追加しても、プロフィールpicの返信リンクが機能していなくても、GoogleはメールIDを返していません。私はこの質問に様々な答えを見てきましたが、すべてuserinfo.emailを含めるように言われています.itは廃止されました。 Googleのドキュメントごとに新しいスコープパラメータは、下記のメール である私のコード あるすべてのヘルプは パスポートGoogle oauthが電子メールパスポート認証を返信しない

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

download.js

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 : '/' 
     })); 

を高く評価している私が持っていました同じ問題があり、このようにスコープを書いた:

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

上記の回答は間違いなく機能しています。これにもう1つの方法があります。 profileであなたのroutes.js

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

emailを追加します。

これで問題が解決するはずです。

関連する問題