Follow this Blog!

Jun 25, 2015

f Comment

How do You Use Facebook API to Get Every Post of a Facebook Fan Page?

Amazon Let's say your Facebook fan page is https://www.facebook.com/Ch2En/ and you'd like to programmatically get every post you have posted on the fan page. How do you do that?

I am using Facebook API version 2.3.

Step 1

First you need to know the fan page's ID, which you can find by adding your fan page URL to FB API as follows:

https://graph.facebook.com/v2.3/https://www.facebook.com/Ch2En/?access_token=<your access token>
{
  "id": "356891854337157",
...
  "were_here_count": 0
}
You get a JSON structure with information about your fan page including fan page ID, fan page description, fan page's like count, how many people were there, etc. The "id" is what you are interested in. In this case, "id" is 356891854337157.

Let me know if you don't know how to get your access token.

Step 2

Knowing the ID, 356891854337157, use the following URL to see the latest post on https://www.facebook.com/Ch2En.

https://graph.facebook.com/v2.3/356891854337157/feed?limit=1&access_token=<your access token>

You'll see a JSON data structure like this one:
{
   "data": [
      {
        "id": "356891854337157_1164021940290807",
        "from": {
            ...
        },
        "message": 
        ...
      }
   ],
   "paging": {
      "previous": "...",
      "next": "..."
   }
}
You can get the ID of the posted message, the actual posted message, a list of people who have liked this message, comments from others, etc. from this JSON structure.

The parameter "limit" is the number of posts retrieved by this API call. If you want to go to the next page, follow the URL in paging -> next.

How do you get the URL of a particular post?

In the JSON data structure above, the id is 356891854337157_1164021940290807. Here's a URL of this particular post:

https://www.facebook.com/Ch2En/posts/1164021940290807

Notice something strange? You only need the number portion after the underscore of the id to get the post URL. This number portion is 1164021940290807 in this case.

How do you crawl comments of a Facebook comments plugin?

To find out how to crawl comments from an FB comments plugin, consult Using Facebook Graph API To Crawl Comments from a Facebook Comments Plugin.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
Find Michael on Google or Facebook
ADVERTISING WITH US - Direct your advertising requests to Michael