How to use Facebook API after authentication with facebook strategy in Passport.js?

I authenticated my user using the Facebook strategy and got user information. My app should now hit the other endpoints of the api chart on Facebook. I see no way to access the tool for sending requests to the Facebook api chart. Having studied the Strategy a little further, I see that everything is built around the OAuth 2 strategy.

1) How to use facebook strategy to call other api chart endpoints?

2) Do I have to wake up in the api passport somewhere in order to access the oauth object associated with it for this to happen?

Or am I thinking about it wrong and should I get a user access token and use another third-party library to request a facebook api?

+1
source share
2 answers

In the latter case, you need an access token, and then you can just send a regular request with vanilla or any lib that you need (I personally like the crash).

It will look like this:

Wreck.get('https://graph.facebook.com/me?access_token=' + access_token, function(err, res, payload) { }); 
+2
source

Facebook Passport Strategy:

AccessToken is returned with the profile.

Suppose you installed 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 in the profile object, which is passed along with pass.serializeUser , which 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) }) 
0
source

Source: https://habr.com/ru/post/1275033/


All Articles