20140505 - victor gau - r to access the social graph from facebook

16
R TO ACCESS THE SOCIAL GRAPH FROM FACEBOOK Victor Gau Lightning Talk @ Kaohsiung useR! Meetup 2014/05/05

Upload: victor-gau

Post on 08-May-2015

602 views

Category:

Data & Analytics


2 download

DESCRIPTION

Some basic concepts of using R to access the social graph from the Facebook.

TRANSCRIPT

Page 1: 20140505 - Victor Gau - R to access the social graph from facebook

R TO ACCESS THE SOCIAL GRAPH FROM FACEBOOK

Victor GauLightning Talk @ Kaohsiung useR! Meetup

2014/05/05

Page 2: 20140505 - Victor Gau - R to access the social graph from facebook

Outline

What is the Social Graph? Graph API Explained What is JSON? “R” to Parse JSON Spider Revisited “R” to Access the Social Graph Using Facebook Developer Tools Topics for Sharing Next Time

Page 3: 20140505 - Victor Gau - R to access the social graph from facebook

What is the Social Graph?

Business Insider: So What The Heck Is The 'Social Graph' Facebook Keeps Talking About?

Page 4: 20140505 - Victor Gau - R to access the social graph from facebook

Graph API Explained

APIs for accessing social graphHTTP requests are sent to access the social

graph (the responses are in JSON format)most requests are sent to

https://graph.facebook.com requests for videos are sent to

https://graph-video.facebook.com

https://developers.facebook.com/docs/graph-api/quickstart/v2.0

Page 5: 20140505 - Victor Gau - R to access the social graph from facebook

Graph API Explainedhttps://www.facebook.com/HTC

Page 7: 20140505 - Victor Gau - R to access the social graph from facebook

What is JSON?

JavaScript Object Notation http://en.wikipedia.org/wiki/JSON

Page 8: 20140505 - Victor Gau - R to access the social graph from facebook

“R” to Parse JSON

Install “rjson” packageinstall.packages(“rjson”)

Load “rjson” packagelibrary(rjson)

Functions to use:fromJSON()toJSON()newJSONParser()

Page 9: 20140505 - Victor Gau - R to access the social graph from facebook

Sample Code - Reading JSONdata = '{

"firstName": "John",

"lastName": "Smith",

"isAlive": true,

"age": 25,

"height_cm": 167.64,

"address": {

"streetAddress": "21 2nd Street",

"city": "New York",

"state": "NY",

"postalCode": "10021-3100"

},

"phoneNumbers": [

{ "type": "home", "number": "212 555-1234" },

{ "type": "fax", "number": "646 555-4567" }

]

}'

person <- fromJSON(data)

person$firstName

person$lastName

Page 10: 20140505 - Victor Gau - R to access the social graph from facebook

Spider Revisited# Load Required Packages

library(RCurl)

library(XML)

# Get Page Source

html = getURL("http://www.ptt.cc/bbs/R_Language/index.html")

# Parse to get information using XPath

xml = htmlParse(html)

Xpath = "//div[@class='title']/a//text()"

titles = xml [Xpath]

Page 11: 20140505 - Victor Gau - R to access the social graph from facebook

“R” to Access the Social Graph

# Load Required Packages

library(RCurl)

library(rjson)

# Retrieve Data (in JSON format)

data = getURL("https://graph.facebook.com/htc", ssl.verifypeer = FALSE )

# Read JSON

htc <- fromJSON(data)

Page 12: 20140505 - Victor Gau - R to access the social graph from facebook

“R” to Access Social Graph Access Token would be needed for

certain content in social graph most of the time.

Access Token can be retrieved by using graph explorer from Facebook Developer Tools.

Sample URL for retrieving information with Access Token:http://graph.facebook.com/me/friends?

access_token=XDFAFDFXXXXXXXDAFDA

Page 13: 20140505 - Victor Gau - R to access the social graph from facebook

Using Facebook Developer Tools

https://developers.facebook.com/tools/

Page 14: 20140505 - Victor Gau - R to access the social graph from facebook

DEMO

Page 15: 20140505 - Victor Gau - R to access the social graph from facebook

Topics for Sharing Next time Authentication Social Plugins Open Graph Protocol FBML XFBML FQL …

Page 16: 20140505 - Victor Gau - R to access the social graph from facebook

Thank you!