PassportJS Profile Undefined Name

混江龙づ霸主 提交于 2019-12-12 02:57:48

问题


I'm using PassportJS and passport-google-oauth in an ExpressJS web app.

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.use(new GoogleStrategy({
  clientID: GOOGLE_CLIENT_ID,
  clientSecret: GOOGLE_CLIENT_SECRET,
  callbackURL: CALLBACK
},
function(accessToken, refreshToken, profile, done) {
  console.log(profile.displayName);
  console.log(profile.name.familyName);
  console.log(profile.name.givenName);
  ...
}));

The problem is that profile.displayName, profile.name.familyName and profile.name.givenName are undefined. When I use the callback with Passport Facebook, no problem at all.

How to get the name of the user when using a Google account to login?


回答1:


When I checked it seems it has more parameters than what is in the official sample leading people to confusion including me..

rather than

function(accessToken, refreshToken, profile, done)

use

function(req, accessToken, refreshToken, profile, done)



回答2:


you need to request for it, include 'https://www.googleapis.com/auth/userinfo.profile' in your scope.

passport.use(new GoogleStrategy({
  clientID: GOOGLE_CLIENT_ID,
  clientSecret: GOOGLE_CLIENT_SECRET,
  callbackURL: CALLBACK,
  scope: ['https://www.googleapis.com/auth/userinfo.profile','email', ...]
}


来源:https://stackoverflow.com/questions/17399635/passportjs-profile-undefined-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!