Facebook Mining

Mr. I
kasta
Published in
4 min readDec 18, 2017

Bangkok, Irsyad — In an article about social media mining, the reader probably expects a chapter about Facebook. Launched in 2004, and initially limited to Harvard University, today Facebook is a multibillion-dollar company with nearly 2 billion monthly active users. Its popularity makes it an extremely interesting playground for data mining. In this series, we will discuss mining posts from an authenticated user; mining Facebook Pages, visualizing posts and measuring engagement, building a word cloud from a set of posts.

Facebook Graph API

The Facebook Graph API is at the core of the Facebook platform, and it’s one the main components that enable the integration of third parties with Facebook. As the name suggests, it offers a consistent graph-like view of data, representing objects and the connections between them. The different platform components allow developers to access Facebook data and integrate Facebook functionalities into third-party applications.

In terms of data mining opportunities, there was a major shift during 2017 with the release of version 2.0 of the API. One of the main objects of interest in data analysis is the social graph, that is, the list of connections (friendship) between users. Since version 2.0 of the Graph API, applications that want to access this information must explicitly ask for the user_friends permission, but the API will only return the list of friends who are also users of the given app. Effectively, this choice has transformed what used to be a goldmine for data analysis. This section discusses the creation of a Facebook app in Python and the basics of the interaction with the Facebook Graph API.

Registering your App

The access to the Facebook API is offered through a registered app. Developers have to register their app in order to obtain the credentials needed to consume the Graph API. As a Facebook user, you explicitly have to register as a developer in order to create apps, and your account has to be verified via either by mobile phone or credit card. From the Facebook Developers website (https://developers.facebook.com/), the procedure is straightforward: clicking on the Add a New App link under the My Apps menu will open the dialogue window as shown in below, where we provide Social Media Mining as the display name for our sample app (there is a 32-character limit):

Once we have selected a name and category for our app, we can click on Create App ID to confirm its creation.

Authentication and Security

In order to access users’ profile information, as well as the information about their interactions with other objects (for example, Pages, places, and so on), your app must obtain an access token with the appropriate permissions. A token is unique to the user-app combination and handles the permissions that the user has granted to the application. Generating an access token requires user interaction, which means that the users have to conform to Facebook (usually via a dialogue window) that they are granting the required permissions to the application.

For testing purposes, another way to obtain an access token is to use the Graph API Explorer (https://developers.facebook.com/tools/explorer), a tool developed by Facebook to provide developers with a convenient interface to interact with the Graph API. The figure below showcases the use of the Graph API Explorer after we have selected our app from the list of available applications, we can click on Get User Access Token from the Get Token menu:

This action will open a dialogue window, like the one in Figure below, that we can use to specify what kind of permissions we want to include in the access token. Confirming the permissions for the app will generate the alphanumeric string for the access token, which will be valid for two hours from the time of its creation. Clicking on the small information icon next to the token will show this information.

As we can see from above figure, the permissions for a Facebook app are particularly fine-grained. In this way, the users who install your app will have full visibility of the kind of data they want to share with your app.

Accessing The Facebook Graph API with Python

Once the app details are defined, we can programmatically access the Facebook Graph API via Python.

Facebook doesn’t provide an official client for Python. Implementing our own client using the requests library could be an interesting exercise in order to understand the peculiarities of the API, but fortunately, there are already some options out there to simplify the process.

For our examples, we’re going to use facebook-sdk, also based on the requests library, which provides an easy-to-use interface to get data from and to a web service. At the time of writing, the latest version of the library available on PyPI is 1.0.0, and it fully supports Python 3. The previous versions have shown some hiccups in terms of Python 3 compatibility. We can install the library using pip from our virtual environment, as follows:

pip install facebook-sdk

After we have obtained a temporary token following the steps described in the previous section, we can immediately test the library. The following script, facebook_my_profile.py, connects to the Graph API and queries for the profile of the authenticated user:

import json
import facebook
if __name__ == '__main__':
token = 'EAAD6QCmdE6EBAKjf0i3iIcEBglPhhaxJelmb3HjZArKRqnhwfWzRho6FRp2JptvhvXMVoqSqBbqoTBkVmlIWV5tICAIenwx9KsqjkMmhbhSYEW4mA2EDJUgCYxwOSlDA2MmTEJv2JAtBGTKMf2BKHjXgySAvNJVCT5lrGxo5qL0gZBtmpGxDv5kVtZCedWzbZAWSEgUsDQZDZD'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me", fields='name,location{general_info,location},languages{name,description}')
print(json.dumps(profile, indent=4))
The script doesn't take any argument, so it can simply be run with the following command:
C:\Users\admin\Desktop\facebook>python facebook_my_profile.pyThe output is a dump of the JSON object returned by the API:{
"name": "Irsyad Jamal Pratama Putra",
"location": {
"location": {
"city": "Brussels",
"country": "Belgium",
"latitude": 50.85,
"longitude": 4.35
},
"id": "108429795855423"
},
"languages": [
{
"name": "English",
"id": "106059522759137"
},
{
"name": "Indonesian",
"id": "105541139480550"
}
],
"id": "1793875093955948"
}
Enjoy the tutorial. 🍼☕

--

--

Mr. I
kasta
Editor for

Code using various programming language commonly based on JVM (Java, Scala, Groovy) with DBMS (Oracle, PostgreSQL & MySQL)