Kyle Decot
Kyle Decot

Reputation: 20835

Facebook Graph API only returning 50 comments

I'm using the koala gem as show in Railscasts episode #361. I'm attempting to get all of the comments of a given Post but Facebook only seems to be giving me back the last 50 comments on the post. Is this a limitation of Facebook's Graph API or am I doing something wrong?

fb = Koala::Facebook::API.new oauth_token
post = fb.get_object(id_of_the_post)
comments = fb.get_object(post['id'])['comments']['data']
puts comments.size # prints 50

Upvotes: 0

Views: 891

Answers (1)

gvoicu
gvoicu

Reputation: 80

Graph API paginates the result when is a larger number of posts than the limit that is set (in your case 50).

In order to access the next page of results, call "next_page" method:

comments = fb.get_object(post['id'])
while comments['comments']['data'].present?
  # Make operations with your results
  comments = comments.next_page
end

Also, by looking in source one can see that "get_object" method receives 3 parameters:

def get_object(id, args = {}, options = {})

This way, you can raise your posts per page to as many posts as you want:

comments = fb.get_object(post['id'], {:limit => 1000})

Upvotes: 4

Related Questions