I have a facebook Graph API call to get a facebook users feed:
dynamic myFeed = await fb.GetTaskAsync(
("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
"message,type,created_time,likes,comments")
.GraphAPICall(appsecret_proof));
The above returns a number of the latest user posts in a while say 21 or maybe 22 posts but not the complete list of user posts. I searched for a way to iterate through a users feed using facebook pagination and I ended up finding this solution which works with facebook Offset pagination.
dynamic myFeed = await fb.GetTaskAsync(
("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
"message,type,created_time,likes,comments")
.GraphAPICall(appsecret_proof), new {limit = "1000", offset = "21" });
This has taken me a step nearer to what I want to achieve but I think this is not the ideal way to do it and also it does not return all of users posts. Is there any workaround? Please help.
P.S: I am using Facebook C# SDK.
UPDATE1: As per Jeremy's answer. It seems that the facebook cursor pagination is the only right option for my requirements. I would like to know if C# facebook sdk provides any feature to iterate over the Next Edges so I can get all feed posts in one call, Is there any possible solution to this? P.S: I've been through facebook API docs many times and I know what exactly the Nodes, Edges and Fields are, the only unfortunate thing is that facebook doesn't support C# SDK as yet and I was unable to find a proper documentation on Facebook C# SDK too.
Finally after doing some researches and reading some blogs I found out that there is no direct API CAlls
from facebook to fetch all user feeder posts in once.
To achieve that functionality either one has to go for infinite scrolling as suggested by Jeremy Thomson or to iterate through different facebook data pages regardless of which facebook pagination
type is supported by the edge
. As far as I want a process without user interference/actions I would definitely go the second option which is iterating through facebook data pages with a while
loop.
To do That we first need our two most important parameters (facebook access_token
+ (facebook appsecret_proof
) as described below:
var appsecret_proof = access_token.GenerateAppSecretProof();
var fb = new FacebookClient(access_token);
Point to be remembered: facebook
access_token
is generated byHttpContext
class.
The facebook API
call will get the users first 25 feeder post as below:
dynamic myFeed = await fb.GetTaskAsync(
("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
"message,type,created_time,likes,comments")
.GraphAPICall(appsecret_proof));
The API
Call above return results in a Json
array and that should be hydrated through the Model View
properties as shown here:
var postList = new List<FacebookPostViewModel>();
foreach (dynamic post in myFeed.data)
{
postList.Add(DynamicExtension.ToStatic<FacebookPostViewModel>(post));
}
Until here everything was clear before, the most important part which is surely fetching all facebook user post is now in action. For that we need to set a string
NextPageUri
to empty as here:
string NextPageURI = string.Empty;
The final part of all is to check if there is another page for the data, If yes should iterate and add the data to the View Model
until there is no page lift as shown here:
while (myFeed.paging != null && myFeed.paging.next != null)
{
NextPageURI = myFeed.paging.next;
var nextURL = GetNextPageQuery(NextPageURI, access_token);
dynamic nextPagedResult = await fb.GetTaskAsync(nextURL.GraphAPICall(appsecret_proof));
foreach (dynamic post in nextPagedResult.data)
{
postList.Add(DynamicExtension.ToStatic<FacebookPostViewModel>(post));
}
}
This helped me to get rid of the problem faced. But yet I have another task in hand to work on. It is the speed of fetching the posts that if posts are morethan 30k would take 10 minutes which is not ideal at least for me.