Hello All !
In this tutorial, I will help learners to learn about “Data Mining on Facebook Data”.
First of all you have to get an access token from your facebook account. This token will allow you to access the data from your account on FB.
Here is how to get it:
Step 1:
Go to https://developers.facebook.com/tools/explorer
Step 2:
When you see the Graph API Explorer as below, select from the dropdown menu and change API Version to “Unversioned“.
Step 3:
Click on “Get Access Token” button and you will see the below pop-up window. Under “User Data Permissions” tab, select all the options (by clicking on check boxes). Do not click on any other button but you should now click on “Friends Data Permissions” tab.
Step 4:
Similar to step 3, click on all the options and then click on “Extended permissions”
Step 5:
Do the same with “Extended Permissions” as in step 3 and 4:
Click on “Get Access Token” and FB then will return you a system generated code.
Step 6:
Copy this Access Token Code:
We are now good to go.
####################################################
Some basic “FB Data analysis using R”
####################################################
# load required packages (please install if not already installed)
> library(Rfacebook)
> library(Rook)
> require(RCurl)
> require(rjson)
> access_token = "PASTE YOUR TOKEN CODE WITHIN BLOCK QUOTES"
> myFB = getUsers("me", token = access_token)
> myFriends = getFriends(access_token, simplify=FALSE)
> myFriends_info <- getUsers(myFriends$id, token=access_token, private_info=TRUE)
> table(myFriends_info$relationship_status)
## If above is not working then you need to follow the following steps
1. Go to "developer.facebook.com/apps" website using your login
2. Create a new App
3. In settings, click +Add Plateform and add the following address:
http://localhost:1410/ and save it.
4. Run the following code as below in R and hit enter key to proceed with
authrization process.
# Setting default working directory
# setwd("E:/RProjects/Facebook")
# load required packages (please install if not already installed)
library(Rfacebook)
library(Rook)
require(RCurl)
require(rjson)
fb_oauth <- fbOAuth(app_id = "Your FB App ID",
app_secret = "Your FB App Secret", extended_permissions = TRUE)
# Saving authorization file on the computer (in default working directory)
save(fboauth, file="fboauth")
# Loading authorization file from the computer (from default working directory)
load("fboauth")
# Getting info about self (as user)
myFB <- getUsers("me", token = fboauth)
# Summarizing info
myFB
myFB$name
head(myFB$id)
# Collecting info about pages I liked
mylikes <- getLikes(user = "me", token = fboauth)
# Viewing a few of the pages I liked
head(mylikes)
## output....
## Engaged In a relationship In an open relationship
## 3 6 2
## It's complicated Married Separated
## 3 68 1
## Single
## 87
####################################################
Some More…
####################################################
# Retrieve list of mutual friendships and return the list for the network (using either Edge List or Adjacency Matrix)
> myFriendsNetwork1 = getNetwork(access_token, format = "edgelist", verbose = TRUE)
//
> myFriendsNetwork1
## Sample Output of Friends in my network in common with others (within my network)
## [,1] [,2]
## [1,] "Silky Prabhakar Ramphal" "Vivek Saxena"
## [2,] "Silky Prabhakar Ramphal" "Nidhi Negi"
## [3,] "Silky Prabhakar Ramphal" "Priya Garg"
## [4,] "Silky Prabhakar Ramphal" "Divyanka Goyal"
## [5,] "Silky Prabhakar Ramphal" "Rabi Narayan Satpathy"
## [6,] "Silky Prabhakar Ramphal" "Rajan Girdhar"
## [7,] "Silky Prabhakar Ramphal" "Gurwinderpal Singh"
## [8,] "Silky Prabhakar Ramphal" "Akhlak Khan"
## [9,] "Silky Prabhakar Ramphal" "Ankush Sharma Anki"
## [10,] "Silky Prabhakar Ramphal" "Minni Khatana"
## [11,] "Silky Prabhakar Ramphal" "Amandeep Kaur"
## [12,] "Silky Prabhakar Ramphal" "Neena Malhotra"
## [13,] "Silky Prabhakar Ramphal" "Ravneet Garcha Bhatti"
## [14,] "Silky Prabhakar Ramphal" "Pranav Ranjan"
## [15,] "Silky Prabhakar Ramphal" "Deepak Singh Bhardwaj"
## [16,] "Silky Prabhakar Ramphal" "Sharma Sourabh"
## [17,] "Vivek Saxena" "Silky Prabhakar Ramphal"
## [18,] "Vivek Saxena" "Kumar Saurabh"
## [19,] "Vivek Saxena" "Neha Shandilya"
## [20,] "Vivek Saxena" "Yogita S Doda"
## [21,] "Vivek Saxena" "Raj Sethi"
## [22,] "Vivek Saxena" "Mandip Saini"
## [23,] "Vivek Saxena" "Nidhi Negi"
## [24,] "Vivek Saxena" "Sakshi Sharma"
## [25,] "Vivek Saxena" "Anand Mani Singh"
## [26,] "Vivek Saxena" "Prince Vohra"
## [27,] "Vivek Saxena" "Ripsy Midha"
## [28,] "Vivek Saxena" "Manoj Goyal"
## [29,] "Vivek Saxena" "Rohit Sharma"
## [30,] "Vivek Saxena" "Priya Garg"
## [31,] "Vivek Saxena" "Rampal Shikha"
## [32,] "Vivek Saxena" "Richa Kunal Jolly"
## [33,] "Vivek Saxena" "Vimal Saini"
## [34,] "Vivek Saxena" "Nikhil Wadhwa"
## [35,] "Vivek Saxena" "Tushinder Preet Kaur Bhullar"
## [36,] "Vivek Saxena" "Shammi Dhiman"
## [37,] "Vivek Saxena" "Pooja Garg"
## [38,] "Vivek Saxena" "Vikas Anand"
## [39,] "Vivek Saxena" "Ajay Singh Chandel"
## [40,] "Vivek Saxena" "Aman Arora"
## [41,] "Vivek Saxena" "Karan Sood"
## [42,] "Vivek Saxena" "Sardar Manpreet Singh"
## [43,] "Vivek Saxena" "Rabi Narayan Satpathy"
## [44,] "Vivek Saxena" "Ujjwal Kumar Ojha"
## [45,] "Vivek Saxena" "Priya Sharma"
## [46,] "Vivek Saxena" "Tarun Gupta"
## [47,] "Vivek Saxena" "Dal Jeet"
## [48,] "Vivek Saxena" "Saurabh Nagpal"
## [49,] "Vivek Saxena" "Neha Bhanote"
## [50,] "Vivek Saxena" "Nidhi Kapila"
## [51,] "Vivek Saxena" "Akshay Bhardwaj"
## [52,] "Vivek Saxena" "Varun Gautam"
## [53,] "Vivek Saxena" "Kumar Prashant"
## [54,] "Vivek Saxena" "Jyoti Gupta"
## [55,] "Vivek Saxena" "Gurwinderpal Singh"
## [56,] "Vivek Saxena" "Akhlak Khan"
## [57,] "Vivek Saxena" "Ruchika Rajinder Gupta"
## [58,] "Vivek Saxena" "Tejbir Kaur"
## [59,] "Vivek Saxena" "Isha Sikka Dhingra"
## [60,] "Vivek Saxena" "Meenu Hooda"
## [61,] "Vivek Saxena" "Yash Sharma"
## [62,] "Vivek Saxena" "Kanika Grover"
## [63,] "Vivek Saxena" "Shelly Dawar"
## [64,] "Vivek Saxena" "Khushboo Shankar Verma"
## [65,] "Vivek Saxena" "Shweta Sumit Kapoor"
## [66,] "Vivek Saxena" "Aamir Hakeem"
## [67,] "Vivek Saxena" "Dilpreet Singh"
## [68,] "Vivek Saxena" "Ashu Malik"
## [69,] "Vivek Saxena" "Sahil Aggarwal"
## [70,] "Vivek Saxena" "Ashish Sharma"
## [71,] "Vivek Saxena" "Vidya Shankar Prasad"
## [72,] "Vivek Saxena" "Shivendra Mathur"
## [73,] "Vivek Saxena" "Tanvee Pathak"
## [74,] "Vivek Saxena" "Nitin Sharma"
## [75,] "Vivek Saxena" "Gagandeep Atwal"
## [76,] "Vivek Saxena" "Saadgi Kawal Preet"
## [77,] "Vivek Saxena" "Harivansh Kumar"
## [78,] "Vivek Saxena" "Shashi Kant Pandey"
## [79,] "Vivek Saxena" "Gurveer Kaur Sandhu"
## [80,] "Vivek Saxena" "Rocky Malhotra"
## [81,] "Vivek Saxena" "Gaurav Kassal"
## [82,] "Vivek Saxena" "Malika Thakral"
## [83,] "Vivek Saxena" "Amandeep Kaur"
## [84,] "Vivek Saxena" "Dimple Sharma"
## [85,] "Vivek Saxena" "Anju Arora"
## [86,] "Vivek Saxena" "Neena Malhotra"
## [87,] "Vivek Saxena" "Ravinder Kumar"
## [88,] "Vivek Saxena" "Rabia Chhabra"
## [89,] "Vivek Saxena" "Shailesh Tiwari"
## [90,] "Vivek Saxena" "Nindy Kaur"
## [91,] "Vivek Saxena" "Avinash Kaur Deol"
## [92,] "Vivek Saxena" "Pranav Ranjan"
## [93,] "Vivek Saxena" "Sandeep Vij"
## [94,] "Vivek Saxena" "Manish Kumar"
## [95,] "Vivek Saxena" "Vikas Garg"
## [96,] "Vivek Saxena" "Lalita Sharma"
## [97,] "Vivek Saxena" "Amar Cheema"
## [98,] "Vivek Saxena" "Dawar Gaurav"
## [99,] "Vivek Saxena" "Nisha S Duvedi"
## [100,] "Vivek Saxena" "Anu Malhotra"
## [101,] "Vivek Saxena" "Mehak Malhotra"
## [102,] "Karan Sharma" "Jatin Khurana"
## [103,] "Karan Sharma" "Yogesh Sharma"
## [104,] "Karan Sharma" "Vishwajeet Singh"
## [105,] "Karan Sharma" "Vikas Anand"
## [106,] "Karan Sharma" "Shelly Bhalla"
## [107,] "Karan Sharma" "Kumar Prashant"
## [108,] "Karan Sharma" "Saurabh Yadav"
## [109,] "Karan Sharma" "Kanika Grover"
## [110,] "Karan Sharma" "Divesh Arora"
## [111,] "Karan Sharma" "Rohit Singh"
## [112,] "Karan Sharma" "Vibhu Babbar"
## [113,] "Karan Sharma" "Tanu Kunal Deo"
## [114,] "Karan Sharma" "Harivansh Kumar"
## [115,] "Karan Sharma" "Vinish Kumar"
## [116,] "Karan Sharma" "Deepak Sharma"
## [117,] "Karan Sharma" "Anshule Handoo"
## [118,] "Karan Sharma" "Abhay Singh"
## [119,] "Karan Sharma" "Shailesh Tiwari"
## [120,] "Karan Sharma" "Pranav Ranjan"
## [121,] "Karan Sharma" "Niraj Awasthi"
## [122,] "Karan Sharma" "Rajandeep Kaur"
## [123,] "Karan Sharma" "Neha Bedi"
## [124,] "Karan Sharma" "Gaurav Arora"
## [125,] "Karan Sharma" "Niraj Awasthi"
## [126,] "Anupriya Ambastha" "Yogesh Sharma"
## [127,] "Anupriya Ambastha" "Satya Prakash"
## [128,] "Anupriya Ambastha" "Vishwajeet Singh"
## [129,] "Anupriya Ambastha" "Gaurav Fidato"
## [130,] "Anupriya Ambastha" "Sakshi Vidit Shrivastva"
## [131,] "Anupriya Ambastha" "Rajnish Singh"
## [132,] "Anupriya Ambastha" "Kanika Grover"
## [133,] "Anupriya Ambastha" "Jasmeet Singh Meeti"
## [134,] "Anupriya Ambastha" "Robin Singh Siwach"
## [135,] "Anupriya Ambastha" "Vishal Cool"
## [136,] "Anupriya Ambastha" "Rohit Singh"
## [137,] "Anupriya Ambastha" "Gaurav Arora"
## [138,] "Anupriya Ambastha" "Vibhu Babbar"
## [139,] "Anupriya Ambastha" "Tanu Kunal Deo"
## [140,] "Anupriya Ambastha" "Harivansh Kumar"
## [141,] "Anupriya Ambastha" "Gaurav Kassal"
## [142,] "Anupriya Ambastha" "Shailesh Tiwari"
## [143,] "Anupriya Ambastha" "Pranav Ranjan"
## [144,] "Anupriya Ambastha" "Harpreet Bedi"
## [145,] "Anupriya Ambastha" "Neha Bedi"
## [146,] "Anupriya Ambastha" "Gaurav Arora"
## [147,] "Anupriya Ambastha" "Vipul Garg"
## [148,] "Ashish Dargan" "Vimal Saini"
## [149,] "Ashish Dargan" "Disha Sz"
## [150,] "Ashish Dargan" "Shammi Dhiman"
#### Truncated to 150 results ####
> myFriendsNetwork2 = getNetwork(access_token, format = "adj.matrix", verbose = TRUE)
> myFriendsNetwork2
FB Data Mining//
## Silky Prabhakar Ramphal Vivek Saxena
## Silky Prabhakar Ramphal FALSE TRUE
## Vivek Saxena TRUE FALSE
## Karan Sharma FALSE FALSE
## Anupriya Ambastha FALSE FALSE
## Ashish Dargan FALSE FALSE
## Kumar Saurabh FALSE TRUE
## Satyapal Singh FALSE FALSE
## Neha Shandilya FALSE TRUE
## Parminder Dhaliwal FALSE FALSE
//
## Karan Sharma Anupriya Ambastha Ashish Dargan
## Silky Prabhakar Ramphal FALSE FALSE FALSE
## Vivek Saxena FALSE FALSE FALSE
## Karan Sharma FALSE FALSE FALSE
## Anupriya Ambastha FALSE FALSE FALSE
## Utkarsh Shrivastava FALSE FALSE FALSE
FB Data Mining//
## Kumar Saurabh Satyapal Singh Neha Shandilya
## Silky Prabhakar Ramphal FALSE FALSE FALSE
## Vivek Saxena TRUE FALSE TRUE
## Karan Sharma FALSE FALSE FALSE
## Anupriya Ambastha FALSE FALSE FALSE
## Ashish Dargan FALSE FALSE FALSE
## Kumar Saurabh FALSE FALSE TRUE
####################################################
Using fbOAuth for longer login sessions:
# go to https://developers.facebook.com/apps and setup the app
app_id = “xxxxxxxxxxxxxxxxxxxxxxxxxx”
app_secret = “xxxxxxxxxxxxxxxxxxxxx”
fb_auth<- fbOAuth(app_id = app_id, app_secret = app_secret, extended_permissions = TRUE)
save(fb_auth, file=”fb_oauth”)
load(“fb_oauth”)
me <- getUsers(“me”, token=fb_oauth, private_info = TRUE)
Currently it is giving an error:
Error in callAPI(query, token) :
(#12) username is deprecated for versions v2.0 and higher
And Rfacebook package is being updated to resolve this issue. I will notify, soon it is updated.
That’s all for now, folks!!
Will revert soon with some more…
Manoj
Bro. Miguel Villarreal said:
Great, thank you for this.
LikeLike
MANOJ KUMAR said:
you are welcome dear…
LikeLike
AdolfoHC said:
Hello Manoj, according to my results myFriends and myFriends_info have the same variables and rows, what makes them different? I could see that they were obtained using different functions.
Regards
Adolfo
LikeLike
MANOJ KUMAR said:
Dear Adolfo,
Please forward me the results in email so that i could interpret or do something…
Regards,
LikeLike
Alexis said:
This … is … amazing
LikeLike
MANOJ KUMAR said:
Thank you.
LikeLike
rijnvos57 said:
Oke Manoj this was a refreshing exercise and working well. Thanks very much for your post!
LikeLike
MANOJ KUMAR said:
You are welcome.
Thanks very much.
Regards
LikeLike
winwin said:
Thanks for the great tutorial, I tried this code a few times already, ending up with the same result each time: I works, but I only receive 2 out of my 500+ Facebook friends (always the same two). Why is this? Is there some permission my friends have to set in order to get minded?
I am thankful for all helping comments…
LikeLike
MANOJ KUMAR said:
Thanks for liking the post.
I will suggest you to use fbOAth instead… if you can not, do email me so that i could reply to you.
LikeLike
winwin said:
I am not actually sure what you mean with “fbOAth”, so can you provide me some more background information? If you can tell me were I find your mail address we can also chat in private, but I already heard about other users (on other blogs) talking about the same issues… so maybe it it worth discussing it in public.
btw, thanks for your help 😉
LikeLike
MANOJ KUMAR said:
OK I will update the post…
LikeLike
Babu said:
Hi,
Good to have the information, I have tired the code but i am getting the following error message
“Error in function (type, msg, asError = TRUE) : couldn’t connect to host” when i run the below command
> myFB = getUsers(“me”, token = access_token)
Apart from that do we need to get the authorization code every time?
LikeLike
MANOJ KUMAR said:
yes, access token expires in 2 hours and you need to refresh the code in FB account.
for longer sessions, please use fbOAuth instead.
for error, please check the API access (did you tick all the options before generating the access token?)
LikeLike
Sukhbir Singh Bal said:
Hi Manoj
i had run the code it is working ,if you want some help this is email id sukhbirsinghbal@gmail.com
I follow your twitter data mining, it is very good,thanks very much for sharing.
LikeLike
MANOJ KUMAR said:
Thanks dear..
LikeLike
Nikunj said:
These examples are great……thanks for helping..
LikeLike
Nikunj said:
I am getting an error : Error in getUsers(myFriends$id, token = access_token) :
An unknown error occurred. Querying too many users?
What does that mean ? And how to solve it ?
LikeLike
Matt Oldach said:
I tried this code but when I do the following line
table(myFriends)info$relationship_status) i get table of extent 0
I read somewhere else that If you created the app after 4/30/2014 you will only be able to get data for users that are using your app. – “WizKid May 12 at 20:09″
Check developers.facebook.com/docs/apps/changelog
yet, winwin said he got 2 out of 500 friends on August 16, 2014 at 4:12 pm. How did this happen?
LikeLike
MANOJ KUMAR said:
I was out of scene for a while.. I will have to check up those updates from FB… will revert soon. Thanks for reminding me…
LikeLike
Matt Oldach said:
forgot to set a e-mail notification for responses
LikeLike
rajeevpareek said:
Hi Manoj,
Thanks for wonderful tutorial.
I did exactly mentioned by you however getting below error –
“Error in callAPI(query, token) :
(#12) username is deprecated for versions v2.0 and higher”
Can you please help me.
Thanks in advance,
Rajeev
LikeLike
MANOJ KUMAR said:
You have not followed instruction on “facebook API settings”, please follow the steps and select API Version to “Unversioned“
LikeLike
lee said:
Hi Manoj,
I followed the steps but still got the same error:
> myFB = getUsers(“me”, token = access_token)
Error in callAPI(query, token) :
(#12) username is deprecated for versions v2.0 and higher
I found that the token doesn’t change at all when I change my API version from v2.2 to unversioned. I did followed the same way as you did to get my token then run :
myFB = getUsers(“me”, token = access_token)
which gave me the error
Error in callAPI(query, token) :
(#12) username is deprecated for versions v2.0 and higher
Could you spend some time to look into this problem?
Thank you very much~!~!~!
LikeLike
Abhilash said:
Sir can you help to write a code where we can mine the text from a facebook page and perform sentiment analysis.
LikeLike
Sayantan said:
searchFacebook() function is not working ..then how to run sentiment analysis from facebook public posts?
LikeLike
MANOJ KUMAR said:
I guess there might be some changes in the updated package or in FB APIs.. for long I am not following FB text or sentiment analysis. Will try to look into the problem soon. thanks for reminding me.
LikeLike
Rakesh Mishra said:
Hi Manoj,
Thanks such a nice article. But am not able to get data from my FB accouny. I tried both the methods mentioned by you. using myFB = getUsers(“me”, token = accessToken) and myFB <- getUsers("me", token = fb_oauth)
1st one is giving error: Error in callAPI(query, token) :
(#12) username field is deprecated for versions v2.0 and higher. I could not find the option to set API version so I assume 2.4 is being used.
For 2nd option, I am getting Error in callAPI(query, token) :
An active access token must be used to query information about the current user.
I am getting Authentication complete. message for 2nd option.
Can you help with error message?
Regards,
Rakesh
LikeLike
Lukmanul Hakim said:
hi.,.i really interesting about facebook mining but i have problm about it. when i was running in r i get the information error. can you help me?
> my_friends <- getFriends(access_token, simplify=TRUE)
Only friends who use the application will be returned
See ?getFriends for more details
Error in getFriends(access_token, simplify = TRUE) :
No friend information is available. See ?getFriends for more details.
LikeLike
Shakeeb Rahman said:
hi manoj
can i get this in java
LikeLike
MANOJ KUMAR said:
You can.. but not sure… why not try rJava or some other package.
LikeLike
Ashutosh Kumar said:
hi.,.i really interesting about facebook mining but i have problm about it. when i was running in r i get the information error. can you help me?
> my_friends <- getFriends(access_token, simplify=TRUE)
Only friends who use the application will be returned
See ?getFriends for more details
Error in getFriends(access_token, simplify = TRUE) :
No friend information is available. See ?getFriends for more details.
LikeLike
MANOJ KUMAR said:
check for the package update information please. since this blog was written long ago, there are chances that latest updated package might have changed the ways the functions work..
LikeLike
Ashutosh Mehta said:
> my_friends <- getFriends(access_token, simplify=TRUE)
Only friends who use the application will be returned
See ?getFriends for more details
Error in getFriends(access_token, simplify = TRUE) :
No friend information is available. See ?getFriends for more details.
LikeLike
Ashutosh Mehta said:
Only friends who use the application will be returned
See ?getFriends for more details
Error in curl::curl_fetch_memory(url, handle = handle) :
Couldn’t resolve host name
LikeLike
Newbie Enterpreneur said:
hello can we make network for our friends likes what page
may be some like
albert like BMW and Ferrari
shane like Porche , BMW and Ferrari
thanks
LikeLike
DataScientist said:
yes you can, but I am not sure if in free credentials you could. you have to explore upto what extent FB will allow you to go further in data mining.
LikeLike
Newbie Enterpreneur said:
ok thanks, so i must set my apps so i can track the page that my friends likes
LikeLike
DataScientist said:
yes, all the best.
LikeLike