Search code examples
node.jsfacebookfacebook-graph-apipassport.jspassport-facebook

How to get Facebook friends list using passport js?


I know there are few answer out there but it is not working in my case. Actually My code is working by getting basic facebook user info but i want to get user friends.And I defined user_friends scope but not getting user_friends data this is my code

import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware'
import webpackHotMidleware from 'webpack-hot-middleware';
import bodyParser from 'body-parser';


    import webpackConfig from '../../webpack.config.dev';

    //Login Stuff
    import passport from 'passport';
    import config from './Auth';
    const FacebookStrategy = require('passport-facebook').Strategy;



    let app = express();
    app.use(bodyParser.json());
    app.use(express.static('public'))


    const compiler = webpack(webpackConfig);

    app.use(webpackMiddleware(compiler, {
        hot: true,
        publicPath: webpackConfig.output.publicPath,
        noInfo: true
    }));

    app.use(webpackHotMidleware(compiler));

    //FacebookConfigStarts
    passport.use(new FacebookStrategy({
            clientID: config.facebookAuth.clientID,
            clientSecret: config.facebookAuth.clientSecret,
            callbackURL: config.facebookAuth.callbackURL,
            profileFields: ['id', 'displayName', 'photos', 'email']
        },
        function(accessToken, refreshToken, profile, done) {


            console.log(profile)


        }
    ));



    app.get('/auth/facebook', passport.authenticate('facebook',{ scope: ['email','user_friends','manage_pages'] }));


    app.get('/auth/facebook/callback',
        passport.authenticate('facebook', { successRedirect: '/profile',
            failureRedirect: '/login'

        }));



    //FacebookConfigEnds
    app.get('/*', (req, res) => {
        res.sendFile(path.join(__dirname, '../../index.html'))
    });


    app.listen(3000, () => {
        console.log('Listening')
    });

Solution

  • This code snippet works on my application

        importFacebook = (user, accessToken, cb: () => void) => {
        var options = {
            host: 'graph.facebook.com',
            port: 443,
            path: '/v2.9/' + user.facebook.id + '/taggable_friends?access_token=' + accessToken, //apiPath example: '/me/friends'
            method: 'GET'
        };
    
        var buffer = ''; //this buffer will be populated with the chunks of the data received from facebook
        var request = https.get(options, function (result) {
            result.setEncoding('utf8');
            result.on('data', function (chunk) {
                buffer += chunk;
            });
    
            result.on('end', function () {
                console.log(buffer);
                cb();
            });
        });
    
        request.on('error', function (e) {
            console.log('error from facebook.getFbData: ' + e.message)
            cb();
        });
    
        request.end();
    };