【问题标题】:Retrieving photo from Facebook using passport-facebook使用 passport-facebook 从 Facebook 检索照片
【发布时间】:2013-12-01 03:22:48
【问题描述】:

我可以通过 passport-facebook 检索基本用户信息,遵循以下代码并保存在 mongodb 中:

app.get("/auth/facebook", passport.authenticate("facebook", { scope : ["email", "publish_stream", "user_location", "user_hometown", "user_birthday", "read_friendlists"]}));

app.get("/auth/facebook/callback", passport.authenticate("facebook",{ successRedirect: '/', failureRedirect: '/'}));

var mongoose = require('mongoose'), 
FacebookStrategy = require('passport-facebook').Strategy, 
Users = mongoose.model('Users');

module.exports = function (passport, config) { 
passport.serializeUser(function(user, done) { 
    done(null, user.id);
}); 

passport.deserializeUser(function(id, done) { 
    Users.findOne({ _id: id }, function (err, user) { 
        done(err, user); 
    });
});

passport.use(new FacebookStrategy({ 
    clientID: config.facebook.clientID,
    clientSecret: config.facebook.clientSecret,
    callbackURL: config.facebook.callbackURL 
}, function(accessToken, refreshToken, profile, done) { 
    Users.findOrCreateFaceBookUser(profile, done);
}));};

但是,我无法在“个人资料”中看到个人资料图片。

文档https://github.com/jaredhanson/passport-facebook 说要检索照片,我们需要传递 profileFields,如下所示。但是这样做,我可以看到照片 URL,但会丢失 _json 中包含的其他数据,例如profile._json.location.name。如何检索其他用户信息完整的照片?

passport.use(new FacebookStrategy({
// clientID, clientSecret and callbackURL
profileFields: ['id', 'displayName', 'photos', ....]},// verify callback));

【问题讨论】:

  • 你有没有想过这个?
  • 是的,可以像这样使用访问令牌通过图形api访问图片。 "graph.facebook.com" + profile.username + "/picture" + "?width=200&height=200" + "&access_token=" + accessToken;无需使用配置文件字段。

标签: node.js express passport.js passport-facebook


【解决方案1】:

2020解决方案

从 passportjs passport-facebook 策略中获取用户 accessToken。
使用此令牌获取带有用户头像 url 的 json:

https://graph.facebook.com/me/picture?access_token=${accessToken}&&redirect=false

【讨论】:

    【解决方案2】:

    作为answer,使用此代码会更好。

    passport.use(new FacebookStrategy({
      clientID: FACEBOOK_APP_ID,
      clientSecret: FACEBOOK_APP_SECRET,
      callbackURL: FACEBOOK_APP_CALLBACK,
      profileFields: ['id', 'displayName', 'picture.type(large)', 'email', 'birthday', 'friends', 'first_name', 'last_name', 'middle_name', 'gender', 'link']
    }, (accessToken, refreshToken, profile, cb) => {
      const picture = `https://graph.facebook.com/${profile.id}/picture?width=200&height=200&access_token=${accessToken}`
      //
    }))
    

    【讨论】:

      【解决方案3】:

      如果您需要更大的图像(上面 miksii 示例中的默认值是 50px x 50px,非常小),请使用:

      profileFields: ['id', 'displayName', 'name', 'gender', 'picture.type(large)']
      

      然后

      picture: profile.photos ? profile.photos[0].value : '/img/faces/unknown-user-pic.jpg'
      

      这将为该用户返回 200 像素 x 200 像素的个人资料图片。

      【讨论】:

      • 顺便说一句,你不能同时使用 'picture.type(large)' 和 'photos'
      【解决方案4】:

      除了回答您的问题 - 您不必那样做。正如您所提到的,您可以为 Facebook 个人资料定义所需的属性:

        clientID: "...",
        clientSecret: "...",
        callbackURL: "...",
        profileFields: ['id', 'displayName', 'name', 'gender', ..., 'photos']
      

      你能做的只是简单地获取给定属性的值。假设您要创建一个包含该值的属性:

      picture: profile.photos ? profile.photos[0].value : '/img/faces/unknown-user-pic.jpg'
      

      这被证明是一个更好的解决方案,因为某些用户或有时用户名的值可能是未定义的。

      我希望你也觉得这很有用,

      谢谢。

      【讨论】:

      • 我知道这已经有一年多了,但是你能解释一下为什么你使用这个结构吗?和 : 而不是仅仅设置一个值?试图了解发生了什么,谢谢:)
      • 缩短 if - else 条件结构在哪里?表示“如果 profile.photos 返回 true”, : 表示“如果 profile.photos 返回 false”。关于这一点,如果没有照片,我们想为我们的用户设置默认图片(此图片位于指定路径 img/faces/unknown..jpg 中)。
      • 哦哦哦好的,明白了!谢谢:D
      【解决方案5】:

      是的,可以像这样使用访问令牌通过图形 api 访问图片。 “graph.facebook.com/”; + profile.username + "/picture" + "?width=200&height=200" + "&access_token=" + accessToken;无需使用配置文件字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多