Get ALL Facebook posts using facebook api and paging - facebook

It's my intention to get all past posts. I have created a Facebook app. When this code is executed, I have properly logged in with the correct appid and secret.
function loadPage() { // calls the first batch of records
FB.api("/me/feed",{},function(response) { procBatch(response) } );
}
function procBatch(dat) { // handle this batch, request the next batch
for ( i = 0; i < dat.data.length; i++ ) {
procRow(dat.data[i]); // process this row
}
if ( typeof(dat.paging) != 'undefined' ) {
FB.api(dat.paging.next, {}, function(response){ procBatch(dat); } );
} else {
alert("No more records expected");
}
}
Unfortunately, this only takes me back about six months.
Is it possible to go back further?
Thanks in advance.

Yes, that's possible.
This can be done by increasing the limit of number object per page. I've not tried retrieving ALL the posts, but I am pretty sure that you can easily retrieve posts which are even few years old.
You can call /me/feed?limit=numberOfObjectsPerPage instead of just /me/feed to increase this limit.
You can find more information on this here. Do have a look at the Time Based Pagination on the link. This will explain you how to manage the output for the feeds.

Related

How do I paginate results from the Facebook SDK friends endpoint?

I'm using the Facebook SDK for JS to get a list of FB friends for a given FB user ID. Essentially, I make a request as follows:
FB.api(`/${fbUserId}/friends`, (response) => {
if (response && response.data) {
fbFriends = fbFriends.concat(response.data);
}
if (response && response.paging && response.paging.cursors && response.paging.cursors.after) {
// Recurse through.
}
});
I can get the first set of results, which contains 13 friends, and can also see that there are 200+ friends total, but the problem I'm having is when I try to recurse through and get all 200+ friends.
Specifically, I'm referencing the following two pages:
https://developers.facebook.com/docs/graph-api/using-graph-api/#cursors
https://developers.facebook.com/docs/graph-api/reference/user/friends/
Trying to put things together from those two pages, I thought that a request URL like the following would work:
const afterParam = response.paging.cursors.after ? `&after=${response.paging.cursors.after}` : '';
FB.api(`/${fbUserId}/friends${afterParam}`, (response) => {
...
But it doesn't return any results. I am console-logging response.paging.cursors.after, and I'm sure it's set appropriately from the first API call, but it's not returning any more friends. I always get back an empty array for response.data.
What am I doing wrong? Admittedly, the FB API docs seem a bit sparse, and I don't see anywhere where they specifically show how to go through pages of results for the friends API endpoint, but I'm obviously missing something. Thank you.

Azure Mobile Services Node.js update column field count during read query

I would like to update a column in a specific row in Azure Mobile Services using server side code (node.js).
The idea is that the column A (that stores a number) will increase its count by 1 (i++) everytime a user runs a read query from my mobile apps.
Please, how can I accomplish that from the read script in Azure Mobile Services.
Thanks in advance,
Check out the examples in the online reference. In the table Read script for the table you're tracking you will need to do something like this. It's not clear whether you're tracking in the same table the user is reading, or in a separate counts table, but the flow is the same.
Note that if you really want to track this you should log read requests to another table and tally them after the fact, or use an external analytics system (Google Analytics, Flurry, MixPanel, Azure Mobile Engagement, etc.). This way of updating a single count field in a record will not be accurate if multiple phones read from the table at the same time -- they will both read the same value x from the tracking table, increment it, and update the record with the same value x+1.
function read(query, user, request) {
var myTable = tables.getTable('counting');
myTable.where({
tableName: 'verses'
}).read({
success: updateCount
});
function updateCount(results) {
if (results.length > 0) {
// tracking record was found. update and continue normal execution.
var trackingRecord = results[0];
trackingRecord.count = trackingRecord.count + 1;
myTable.update(trackingRecord, { success: function () {
request.execute();
});
} else {
console.log('error updating count');
request.respond(500, 'unable to update read count');
}
}
};
Hope this helps.
Edit: fixed function signature and table names above, adding another example below
If you want to track which verses were read (if your app can request one at a time) you need to do the "counting" request and update after the "verses" request, because the script doesn't tell you up front which verse records the user requested.
function read(query, user, request) {
request.execute( { success: function(verseResults) {
request.respond();
if (verseResults.length === 1) {
var countTable = tables.getTable('counting');
countTable.where({
verseId: verseResults[0].id
}).read({
success: updateCount
});
function updateCount(results) {
if (results.length > 0) {
// tracking record was found. update and continue normal execution.
var trackingRecord = results[0];
trackingRecord.count = trackingRecord.count + 1;
countTable.update(trackingRecord);
} else {
console.log('error updating count');
}
}
}
});
};
Another note: make sure your counting table has an index on the column you're selecting by (tableName in the first example, verseId in the second).

Limit of 100 items for graph statuses?

I'm working on a console application to download statuses and such from my own account -- nothing production or public. I'm finding that I can only get the last 100 statuses, but I was hoping to at least go a couple of years back.
I'm using the C# API, with something like:
dynamic response = Client.Get(string.Format("{0}/statuses", Secrets.FacebookUserName));
while (response.data.Count > 0)
{
foreach (dynamic status in response.data)
{
// do stuff
}
response = Client.Get(response.paging.next);
}
This works fine, but stops after 100 records.
I see the same thing when trying to use FQL:
dynamic x = Client.Get("fql", new { q = "select message from status where uid=me() limit 1000" });
Do I need to go down the road of exploring the batch API?

How to get the post of the user using javascript sdk

I am creating my first facebook app using javascript sdk. In that i can able to post to the user's wall using(FB.api('me/feed', 'post', )).
Now what I need is, when the user accessing my app, it has to show (or list) the post of that user. The code is
FB.api('/me/posts', function(response) {
for (var i=0, l=response.length; i<l; i++) {
var post = response[i];
alert('The value of post is:'+post);
if (post.message) {
alert('Message: ' + post.message);
} else if (post.attachment && post.attachment.name) {
alert('Attachment: ' + post.attachment.name);
}
}
});
But it is not working. If I remove l=response.length and change the condition as i<5 it is going inside the loop but it gives the value of post is undefined.
I didn't get why it is returning as undefined. It returns same for post.message also.
I am getting the access_token of the user also.
If I want to get the post of my user what code i have to use. Can anyone help me in this.
Thanks in advance
You can use the Facebook Graph API Explorer to better understand what data you will be receiving. https://developers.facebook.com/tools/explorer/
You should have l=response.data.length and var post = response.data[i];. Also, some posts will not have a "message", but will have a "story" instead (mainly posts like "Joe Smith and Tom Someone are now friends."). Make sure you also have the appropriate permissions (e.g. user_status) for the information you're trying to receive.

How do I foreach Requests 2.0 callback items using json and javascript?

I'm a noob to JSON, and would like to do a foreach through all the variables that are given to the callback after a user of my Facebook application invites many users to it, using Requests 2.0
According to Facebook, this is the callback that is received.
{
"request_ids": [
0: "[request_id]",
1: "[request_id]"
]
}
When I try to validate that with JSON Lint, it comes up as invalid. That doesn't help me at all. I understand that the JSON is stored in a variable called response. You can look that up here.
I want to do a foreach of all the request id's so that it'll go something like this. Damn I don't know how to do for/foreach loops in javascript.
for (response["*"] key in response){
document.write("ajaxcontrols.php?createSlapAction=REQUEST_ID");
}
Do a loop like this:
for(var i = 0; i < response.request_ids.length; i++) {
alert("Invitation ID: " + response.request_ids[i]);
}