How to use Facebook Graph API after authenticating with Passport.js facebook strategy?
Asked Answered
G

2

1

I've authenticated my user with the Facebook strategy and obtained their user info. My application now needs to hit other graph api endpoints on Facebook. I don't see a way to access a tool to send requests to the Facebook graph api. Upon inspecting the Strategy a little further, I see everything is built around the OAuth 2 strategy.

1) How do I use the facebook strategy to call other graph api endpoints?

2) Am I supposed to drill into the passport api somewhere to access a related oauth object somewhere to make this happen?

Or am I thinking about this wrong and I should be getting the user's access token and using another 3rd party library for querying the facebook api?

Gony answered 9/2, 2015 at 7:56 Comment(0)
M
2

The latter one, you just need the access token and then you can just send a normal request with vanilla or every lib you'd like (I personally like wreck).

That would look like this then:

Wreck.get('https://graph.facebook.com/me?access_token=' + access_token, function(err, res, payload) { });
Melia answered 9/2, 2015 at 10:42 Comment(0)
S
0

With 'passport-facebook' strategy :

The accessToken is returned along with the profile.

Lets say you have setup the following :

// .: Passport Strategy :.
const Strategy = require('passport-facebook').Strategy;
passport.use(new Strategy({
  clientID:"IDxxxxxxxxxxxxxxxxxxx",
  clientSecret:"SECRETxxxxxxxxxxxxxxxxxxx",
  profileFields: ['id', 'displayName', 'name', 'picture.type(large)', 'emails'],
  callbackURL:"http://localhost:1337/login/facebook/return"
 }, FacebookAccess )
)
function FacebookAccess(accessToken, refreshToken, profile, cb){
  // accessToken  : valid FB.GraphAPI token
  // refreshToken : undefined for Facebook
  profile.token = accessToken
  return cb(null, profile);
}

The token is included to the profile object that is passed along to the passport.serializeUser function that you decide, for example :

passport.serializeUser(function(user, cb){
  var platform_user = {
    fbid:user.id,
    name:user.displayName,
    mail:user.emails[0].value,
    token:user.token,
    avtr:user.photos[0].value
  }
  cb(null, platform_user)
})
Sallust answered 19/2, 2018 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.