Passportjs - 如何使用Google策略请求用户的图像?

Nic*_*ick 3 google-api node.js passport.js

如何配置我的Google API访问权限以在进行身份验证时请求用户的图像?目前,"个人资料"仅在用户成功通过身份验证后包含以下属性:

profile.identifier :(字符串)

profile.displayName :(字符串)

profile.emails :(对象)

名称:(对象)

是否可以请求用户的帐户图像?这是我目前的Passport/Google策略配置:

passport.use(new GoogleStrategy({
    clientID: CLIENT_ID,
    clientSecret: CLIENT_SECRET, 
    returnURL: 'http://localhost:3000/auth/google/return',
    realm: 'http://localhost:3000'
  },
  function(identifier, profile, done) {
    console.log('identifier ' + identifier)
    for(var p in profile){
        console.log(p + ' : ' + profile[p])
        if(p === 'name'){
            for(var n in profile[p]){
                console.log(n + ' : ' + profile[p][n])
            }
        }
    }
  }
));
Run Code Online (Sandbox Code Playgroud)

您可以看到我正在检查配置文件以查看返回的信息.我认为这需要在我的Google Api控制台中以某种方式进行配置.这是Google+ api功能吗?

Eug*_*ace 7

护照返回的配置文件对象只映射了几个字段:

profile.id = json.id;
profile.displayName = json.name;
profile.name = { familyName: json.family_name,
                 givenName: json.given_name };
profile.emails = [{ value: json.email }];
Run Code Online (Sandbox Code Playgroud)

但它确实会给你一个_json包含更多信息的属性:

尝试:

var picture = profile._json['picture'];
Run Code Online (Sandbox Code Playgroud)

  • 您正在使用Google OpenID策略.请尝试使用OAuth2.返回我在答案中提到的_json属性:https://github.com/jaredhanson/passport-google-oauth (2认同)