I want to extract facebook comments from newsarticle website. for example, I want to extract comments from the link http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls. Using the graph api from facebook, here is the result: http://graph.facebook.com/comments/?ids=http://therealsingapore.com/content/16-year-old-schoolboy-had-sex-3-underage-girls.
I would like to extract the comments using nodejs. I have used one open source module called request to get the result as an http object
Here is my code: I use JSON.parse to convert the string into a JSON Obj. However, I failed to get attributes such as id or message from the parsedResponse variable. Can anyone tell me where went wrong? Or is there a better way to get facebook comments with nodejs? Thank you!
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);
});
This is IMHO only possible if you use the Batch API (https://developers.facebook.com/docs/graph-api/making-multiple-requests#operations) like this:
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/
In Node, this would equivalent in
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);
}
});
You need to replace {your_app_access_token}
with a real App Access Token.