LAB EXERCISE

ANALYZE FACEBOOK FAN PAGE

2016.8.26   Luna

what will we do?

  • Use R to analyze a public Facebook Page

How do we DO it?

1. How 3rd party Apps get access to Facebook?

   

 

 

2. How R retrieve data from Facebook Page?

 

 

3. How R visualize data as word-cloud?

   


    - Facebook Graph API
    - Get authentication (OAuth)

    - Rfacebook - access Facebook Graph API

​    - wordcloud  - generate word-cloud

Access to FACEBOOK

Facebook graph api

  • The primary way to get data in and out of Facebook
  • HTTP based API
  • Most Graph API requests require the use of access tokens
GET /v2.5/me HTTP/1.1
Host: graph.facebook.com

Sample request :

Sample response :

{
  "me": {
    "id": "1234567890"
    ... // Other fields
  }, 
  "platform": {
    "id": "19292868552"
    ... // Other fields
  }
}

Obtaining access tokens

  • A temporary token (valid for two hours)
  • A 'long-lived' token (valid for two months)
    • By creating a Facebook APP

To get a temporary token

Obtain Short-Lived Access Token

    – Go to the Graph API Explorer
       https://developers.facebook.com/tools/explorer
    – From the Application pull-down menu, select the application that would be used to obtain the access token
    – Click "Get Token” → “Get User Access Token"
    – In the "Select Permissions" pop-up, select required permissions
    – Click "Get Access Token" button

    – Save value of Access Token for usage of our following R exercise

Create a Facebook App

To get a long-lived token

Create Facebook App - Step 1

CREATE FACEBOOK APP - Step 2

    – Registry as a Facebook developer

CREATE FACEBOOK APP - Step 3

    – Click “My Apps” >> “Add a New App”​​

CREATE FACEBOOK APP - Step 4

    – Fill in fields below and click “Create App ID”

CREATE FACEBOOK APP - Step 5

    – Go “Settings” >> “Basic”

    Click “+ Add Platform” bar near bottom of the page

CREATE FACEBOOK APP - Step 6

    Choose “Website” when the “Select Platform” menu pop-up

    – Set "Site URL" to http://localhost:1410/

    – Don't forget to click “Save Changes” button.

CREATE FACEBOOK APP - DONE

   – Click “Show” button to see App Secret

   Save App ID and App Secret for usage of our following R exercise. We'll use them to obtain a long-lived access token.

 

Referencen

R EXERCISE

Install and Load Required Packages

# Install
install.packages("Rfacebook") # access to Facebook API 
install.packages("wordcloud") # word-cloud generator 

# Load
library("Rfacebook")
library("wordcloud")

OBtain Access token - short-lived token

# Get a short-lived token
fb_oauth <- "EAACEdEose0cBABRTSRzYWHxJn8K3snf31vaiPTbZBkH8YdWe8l
21RZCORW7IgB3EVjbRkqmglvhy9qfYLrxaak8TkwQacHUto1ZBwAgR1QEp3XIltw
UMgi8e1ucKkifYugaIRfnc94QnQ7fCnGWjpBVVDWSZBSKb2ayjsP7xn8OPT2iAGJ0v"

Assign value to the temporary access token you get from Graph API Explorer

OBtain Access token - Long-lived token

fb_oauth <- fbOAuth(app_id="330925050572961", 
                app_secret="5316c301f789e9da6832eb8f0dca10c2")

Replace app_id and app_secret with values of Facebook App you created

OBTAIN ACCESS TOKEN - LONG-LIVED TOKEN

GRAND APP ACCESS to Facebook

Hit enter

 

 

R console will show following message:

URL :

https://www.facebook.com/dialog/oauth?client_id=330925050572961&scope=public_profile%20user_friends&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F&response_type=code&state=QwCpMgWkb8

A browser window should open and you have to allow the app to access your Facebook account

Guery user info.

me <- getUsers("me", token=fb_oauth)

me$name
view(me)

Extract posts from a public Facebook page

& generate wordcloud

# Get information from a public Facebook page
page <- getPage(page="YahooTWNews", token=fb_oauth, n=30, 
                since='2016/01/01', until='2016/08/31')
page[which.max(page$likes_count), ]

# Generate wordcloud with frequencies
wordcloud(page$message, page$comments_count)
wordcloud(page$message, page$likes_count)

Thank you

R_LAB1

By App Lulee

R_LAB1

  • 430