2016.8.26 Luna
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
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
}
}
– 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
– Registry as a Facebook developer
– Click “My Apps” >> “Add a New App”
– Fill in fields below and click “Create App ID”
– Go “Settings” >> “Basic”
– Click “+ Add Platform” bar near bottom of the page
– Choose “Website” when the “Select Platform” menu pop-up
– Set "Site URL" to http://localhost:1410/
– Don't forget to click “Save Changes” button.
– 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.
# Install
install.packages("Rfacebook") # access to Facebook API
install.packages("wordcloud") # word-cloud generator
# Load
library("Rfacebook")
library("wordcloud")
# Get a short-lived token
fb_oauth <- "EAACEdEose0cBABRTSRzYWHxJn8K3snf31vaiPTbZBkH8YdWe8l
21RZCORW7IgB3EVjbRkqmglvhy9qfYLrxaak8TkwQacHUto1ZBwAgR1QEp3XIltw
UMgi8e1ucKkifYugaIRfnc94QnQ7fCnGWjpBVVDWSZBSKb2ayjsP7xn8OPT2iAGJ0v"
Assign value to the temporary access token you get from Graph API Explorer
fb_oauth <- fbOAuth(app_id="330925050572961",
app_secret="5316c301f789e9da6832eb8f0dca10c2")
Replace app_id and app_secret with values of Facebook App you created
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
me <- getUsers("me", token=fb_oauth)
me$name
view(me)
# 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)