使用带有节点的facebook图表api获取性别和年龄

时间:2015-08-21 23:01:37

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

我正在尝试使用Node.js使用Facebook Graph API获取用户的性别和生日。我的问题是只显示用户的ID和名称,我不知道原因。

这是我的代码,直到现在:

Facebook.js

    var https = require('https');

exports.getFbData = function(accessToken, apiPath, callback) {
    var options = {
        host: 'graph.facebook.com',
        port: 443,
        path: apiPath + '?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(){
            callback(buffer);
        });
    });

    request.on('error', function(e){
        console.log('error from facebook.getFbData: ' + e.message)
    });

    request.end();
}

这就是我所说的:

 facebook.getFbData(access_token, '/me/friends', function(data){

此代码仅返回用户ID和姓名,我该如何获得他的性别和年龄?

1 个答案:

答案 0 :(得分:0)

您需要稍微改变一下方法以传递更多参数。您要请求的路径是: https://graph.facebook.com/me?access_token=access_token&fields=id,name,birthday,gender

相关问题