如何使用护照js获取Facebook好友列表?

时间:2016-11-29 09:24:11

标签: node.js facebook facebook-graph-api passport.js passport-facebook

我知道答案很少,但在我的情况下不起作用。 实际上我的代码是通过获取基本的Facebook用户信息,但我想得到用户的朋友。我定义了user_friends范围,但没有得到user_friends数据 这是我的代码

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

1 个答案:

答案 0 :(得分:3)

此代码段适用于我的应用

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