如何使用nodejs从newsarticle网站中提取facebook评论

时间:2014-09-09 13:05:49

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

我想从newsarticle网站上提取facebook评论。例如,我想从链接中提取评论 http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls。 使用来自facebook的图形api,结果如下: http://graph.facebook.com/comments/?ids=http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls

我想使用nodejs提取评论。我使用了一个名为request的开源模块将结果作为http对象

这是我的代码:我使用JSON.parse将字符串转换为JSON Obj。但是,我无法从parsedResponse变量中获取id或message等属性。谁能告诉我哪里出错了?或者有没有更好的方式来获得与nodejs的Facebook评论?谢谢!

var http = require('http');


var url = 'http://graph.facebook.com/comments/?ids=http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls';

var request = require('request');
var counter = 0;

request(url, function (error, response, body) {

if (!error && response.statusCode == 200) {

  var  parsedResponse = JSON.parse(body);
}
console.log(parsedResponse);
var msg = parsedResponse.comments.data[0].message;
console.log(msg);
});

1 个答案:

答案 0 :(得分:0)

只有在您使用批处理API(https://developers.facebook.com/docs/graph-api/making-multiple-requests#operations)时才能使用IMHO:

curl \
   -F 'access_token={your_app_access_token}' \
   -F 'batch=[{ "method":"GET","name":"get-id","relative_url":"v2.1/?ids=http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls",},{"method":"GET","relative_url":"{result=get-id:$.*.og_object.id}/comments?limit=500"}]' \
   https://graph.facebook.com/

在Node中,这相当于

var request = require('request');
var app_access_token = "{your_app_access_token}";

request({
    url: "https://graph.facebook.com",
    body: "access_token="+app_access_token+"&batch="+JSON.stringify([{ "method":"GET","name":"get-id","relative_url":"/v2.1/?ids=http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls",},{"method":"GET","relative_url":"/{result=get-id:$.*.og_object.id}/comments?limit=500"}]),
    method: "POST"
}, function (error, response, body) {

    if (!error && response.statusCode == 200) {

        var parsedResponse = JSON.parse(body);
        console.log(parsedResponse);

    }

});

您需要使用真实的应用访问令牌替换{your_app_access_token}