Glitches
Glitches

Reputation: 762

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

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?

Upvotes: 1

Views: 838

Answers (2)

EMX
EMX

Reputation: 6211

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)
})

Upvotes: 0

Ben A.
Ben A.

Reputation: 488

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) { });

Upvotes: 2

Related Questions