Get your hand dirty — Facebook Graph API for the first time, PART 1

Kiu Lam
4 min readJun 3, 2017

NOTE: Since Facebook updates its Graph API, thus some information discussed here may not be relevant anymore.

EDIT: I decided to expand the article into two part, the first part would mainly discuss about retrieving data from the Graph Explorer

One of my side-projects involve with the use of Facebook Graph API. In the project, one of the steps require to collect specific data from every feeds posted on my college’s Facebook group. The experience was indeed interesting, thus I decided to write an article about it.

For the #Firstimer, Facebook suggests to use the Graph API Explorer to experiment with their API calls.

Before to do so, I need to obtain the permission for accessing the group through the API. I clicked “Get Token” next the text-field of “Access Token,” and selected “Get User Access Token.”

Facebook Graph API explorer, with an endpoint of /me (My Facebook profile)

Since this is for accessing a Facebook group, thus selected “user_manage_groups.”

Graph API Explorer asking permission to access your Facebook account

Graph API Explorer will ask for permission to access your Facebook account

Yay! Finally, Graph API explorer will assign an user access token to you.

Access Token has been filled with an new user access token

To do a GET request for the feeds of the group, I added an end point “/feed” after the group id, the API then returns a JSON with the key of “data,” which the value contained the list of feed data in object type.

The feed data for Hackathon Hacker

Before start using the API, here are some side notes to remember:

If the Facebook group is “Public Group” , everyone can access the data

Hackathon Hackers is a Public Group

If the Facebook group is “Closed Group”, only the moderators or admins of the group can access the data

Cirque du Twerque is a Closed Group

If the Facebook group is “Secret Group”, that’s mean the group is super duper secret, enough none of us would be able to access their data at all :-(

The group is so secret I can’t reveal the name of it :-[

Let’s extend the idea bit more:

Since it’s called Facebook Graph API, therefore let’s think of the entire structure as a graph, the id node connects to the feed node, and the feed node connects to multiple feed nodes.

As mentioned earlier, I need to collect specific data from each feed. In this case, I want to know who is the poster, and whether the feed had “mentioned” someone.

In order to retrieve these information, assign “from” and “message_tags” to the field, so each of the feed node would contain the corresponding information. By the way, I also set the limit to 200, to retrieve as many result as possible. Afterward, we should have the result below:

A GET request with “from,” “message_tags,” and “limit”

Each feed object now contains the key “from” (the name and id of the poster), and “message_tag”, if the poster has tagged someone in the feed.

Wait a minute, what if you want to collect even more data, for example, the comments from every feed, or even the comments of the comments from every feed??!!!

Turnouts there are two ways to retrieve comment data

First! You can set “comments” as part of the field, along with the end point of the “group_id”, so now the JSON data would also include the comments field, if the feed has comments inside.

Second! Each of the feed contained a “id” key, to indicate the id of the feed, if you perform a GET request with the id as an end point, it will just retrieve the data of that individual feed, thus, just like what we did with “group_id”/“feed” before, if you set the end point as “feed_id”/”comments”, it will retrieve the data of the comment for the corresponding feed!!!

Guess what, you can literally do the same for the comment’s id as well :-)

Okay! Now we know the basic functionality of the Graph API, let’s write a program for it

To be, or not to be, continued…

--

--