Retrieving photo from Facebook using passport-facebook

前端 未结 5 1849
醉梦人生
醉梦人生 2021-02-13 02:44

I am able to retrieve basic user information via passport-facebook, following the below code and saving in mongodb:

app.get(\"/auth/facebook\", passport.authenti         


        
相关标签:
5条回答
  • 2021-02-13 02:59

    Yes, the picture can be accessed via the graph api using the access token like this. "graph.facebook.com/"; + profile.username + "/picture" + "?width=200&height=200" + "&access_token=" + accessToken; There is no need to use the profile fields.

    0 讨论(0)
  • 2021-02-13 03:04

    As this answer, it will be work better with this code.

    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}`
      //
    }))
    
    0 讨论(0)
  • 2021-02-13 03:06

    In addition to answer of your question - you don't have to do it that way. As you mentioned you can define the required attributes for Facebook profile:

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

    What than you can do is just simply grab the value of the given attribute. Let's say you want to make an attribute that will hold this value:

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

    This proved to be a better solution since some users or sometimes the value of username may be undefined.

    I hope you find this useful too,

    Thank you.

    0 讨论(0)
  • 2021-02-13 03:08

    If you need a larger image (default in miksii's example above is 50px x 50px which is pretty small), then use:

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

    and then

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

    This will return a 200px x 200px profile picture for that user.

    0 讨论(0)
  • 2021-02-13 03:11

    2020 solution

    Get users accessToken from passportjs passport-facebook strategy.
    Use this token to get json with a url for an users avatar:

    https://graph.facebook.com/me/picture?access_token=${accessToken}&&redirect=false
    
    0 讨论(0)
提交回复
热议问题