Scraping Facebook Data – A Facebook Graph API Example

March 21, 2017

Scraping Facebook Data – A Facebook Graph API Example

It started by trying to scrape likes from a Facebook video page. I was using an open source gem that scraped like counts from a Facebook video page.

Facebook social media scrabble letters image

Facebook changed the HTML page structure so that the regular expression we were using to scrape like counts kept scraping the wrong field. So we were undercounting likes.

This led me to wonder if I could leverage the Facebook Graph API to pull the like counts instead.

Types of Tokens

To get this data, however, you need a token. I started reading up on the different types of tokens Facebook provides. In particular, this post gives a useful summation of the different types of tokens which I used in my own descriptions of tokens below.

The facebook docs page also is helpful.

User Access Token

The user access token allows you to access the Graph API on behalf of a specific user and allows you to perform “write” actions (i.e., post/update/delete) on behalf of said user.

Apparently, you can generate long-lived tokens via a special code.

Page Access Token

The page access token allows you to perform actions on behalf of a target facebook page (e.g., update/delete information on behalf of the page).

You can get these tokens from a user with administrator privileges on a particular facebook page.

App Access Token

The application access token allows you to perform actions on behalf your facebook application. I think of it as a way to pretend you’re a web application asking to perform actions against the Facebook APIs.

The application access token never expires and is apparently a concatenation of your application id and application secret from facebook.

The Video Graph API

I ended up using the application access token because it seemed the easiest whereby I didn’t have to perform multiple server to server calls to get a longer lived user token.

What follows is the list of steps you need to get an access token to fetch data from the Facebook API.

Step 1 – Register as a Facebook Developer

Register as a facebook developer. Go to https://developers.facebook.com/docs/apps/register.

Step 2 – Jot down the app client id and secret

Once you are registered with an application, you can now use the application id and secret to generate an application access token.

Step 3 – Make an OAuth call to get the app access token

Use a tool like curl or postman to get an application access token.

https://graph.facebook.com/<API_VERSION>/oauth/access_token?
client_id=<YOUR_APP_ID>
&redirect_uri=<YOUR_URL>
&client_secret=<YOUR_APP_SECRET>
&code=<AUTHORIZATION_CODE>

Step 4 – Fetch your data!

Now you can use the Facebook graph API explorer to try and fetch data or write your own tool to do so.

Step 5 – The application access token sometimes doesn’t let you fetch

Depending on the facebook page setup, I’ve found sometimes using the Graph API to fetch video data doesn’t always allow you to get publicly available data. In which case, you may have to resort to scraping HTML.

Summary

There are multiple ways to get data from Facebook. This post just scratches the surface.


Profile picture

Written by Bruce Park who lives and works in the USA building useful things. He is sometimes around on Twitter.