[英]Google oauth not returning email passport authentication
我正在嘗試使用節點 js 的護照模塊使用谷歌按鈕登錄。 我正在嘗試獲取某人的電子郵件 ID、姓名、個人資料圖片。 我正在嘗試將圖片下載到本地服務器。 即使將“電子郵件”添加到范圍后,Google 也不會返回電子郵件 ID,並且返回的個人資料圖片鏈接也不起作用。 我研究了這個問題的各種答案,但都說要包括 userinfo.email。 它現在已被棄用。 根據谷歌文檔,新范圍參數是電子郵件。
下面是我的代碼。 任何幫助表示贊賞。
護照
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);
});
}
});
});
}));
};
路由.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 : '/'
}));
下載.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!");
});
我遇到了同樣的問題,並以這種方式編寫了范圍:
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);
});
我希望這對你有幫助。
上面的答案肯定有效,還有另一種方法可以解決這個問題。
app.get('/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
);
在帶有profile
的routes.js
添加email
。
這應該可以解決您的問題。
根據 oauth 的谷歌文檔,第一個參數必須是 openid,第二個參數可以是電子郵件或個人資料,或兩者兼而有之
app.get('/auth/google',
passport.authenticate('google', {scope: ['openid', 'email', 'profile']})
);
在 callbackUrl 之后添加代碼行。
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.