Folks,
In this blog we will learn the basics of extracting Facebook data using R & Facebook API.
Rfacebook Package: Provides an interface to the Facebook API.
Rfacebook package in R provides functions that allow R to access Facebook’s API to get information about posts, comments, likes, group that mention specific keywords & much more.
Install “Rfacebook” package from CRAN : install.packages(“Rfacebook”)
Step 1: Registering an Application with Facebook.
If you already have an account with Facebook, go to FacebookDeveloper and register.
Click “Register Now” button. After you register as a Facebook developer, you can register a new application.
Register a new application
From FacebookDeveloper click on Apps at the top of the page to go to the application dashboard.
Click the button near the top. Once you are done with the verification process, your application is created. Note down the App Id & App Secret.
Site URL on Facebook App Settings: http://localhost:1410
Step 2: Create OAuth token to Facebook R session.
fbOAuth creates a long-lived OAuth access token that enables R to make authenticated calls to the Facebook API.
fbOAuth(app_id=””,app_secret=”YourAppSecret”)
Saving “my_oauth”as a file to be re-used in future sessions, which can be used as token in functions.
Now the connection is done with the Facebook API & R. So here we goes with some of the basic functions.
If you are getting below error while calling function, please go the bottom of this blog post for fix.
Error in callAPI(query, token) : An active access token must be used to query information about the current user.
function getLikes
getLikes(user, n = n , token): Extract list of liked pages of a Facebook user with page id.
Arguments: user: user name/ID , n: Number of liked pages to return for user.
Use below command in R to get likes.
Here is the my result data set “my_likes”. With three variables: Id, names & website of pages.
function getPage
getPage(page , token, n = n): Extract list of posts from a public Facebook page.
Arguments: page: Page ID or page name, n : Number of posts to return for page.
Example: Extracting 10 posts of Facebook Page “Narendra Modi”.
Facebook Page Id of this page you can get from this link: http://findmyfbid.com/ . See below the gif.
After getting the page id use below command in R to get posts.
Here is my result data set “getpagedata”. With 10 Observation & 10 Variables. Variables like Post with likes_count, share_count & comments_count etc.
function search_groups
searchGroup(“text”,token, n = n): Find any group with its privacy status & Facebook ID.
Arguments: text: text string, n : Number of groups to return.
Use below command in R to search groups.
Here is my result data set “search_groups”. With many observation & 3 Variables.
function getGroup
getGroup(ID, token, n = n): Extract list of posts from a public Facebook page. Whose privacy is open.
Arguments: ID: Group ID , n: Number of posts to return for group.
Example: We have to extract 10 posts from 7th group “Web Scraping and Data mining” present in above image groups.
Use below command in R to get groups.
Here is my result data set. With 10 Observation & 3 Variables.
function searchPages
searchPages(, token, n = n): It Search pages that having a string/keyword.
Arguments: string: any string , n: Number of pages to return
Example: We have to search 10 pages that mention a string “Sports”.
Use below command in R to search pages.
Here is my result data set with 10 Observation & 16 Variables.
function updateStatus
updateStatus(“text”, token)
Arguments: text: any string , token
Use below command in R to update Facebook status.
Result:
Fix for Error in callAPI:
If you are getting below error, please follow below steps to fix this issue.
Error in callAPI(query, token) :
An active access token must be used to query information about the current user.
Run Below Function First :-
fbOAuth <- function(app_id, app_secret, extended_permissions=FALSE, legacy_permissions=FALSE, scope=NULL) { ## getting callback URL full_url <- oauth_callback() full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\\1") message <- paste("Copy and paste into Site URL on Facebook App Settings:", full_url, "\nWhen done, press any key to continue...") ## prompting user to introduce callback URL in app page invisible(readline(message)) ## a simplified version of the example in httr package facebook <- oauth_endpoint( authorize = "https://www.facebook.com/dialog/oauth", access = "https://graph.facebook.com/oauth/access_token") myapp <- oauth_app("facebook", app_id, app_secret) if (is.null(scope)) { if (extended_permissions==TRUE){ scope <- c("user_birthday", "user_hometown", "user_location", "user_relationships", "publish_actions","user_status","user_likes") } else { scope <- c("public_profile", "user_friends")} if (legacy_permissions==TRUE) { scope <- c(scope, "read_stream") } } if (packageVersion('httr') < "1.2"){ stop("Rfacebook requires httr version 1.2.0 or greater") } ## with early httr versions if (packageVersion('httr') <= "0.2"){ facebook_token <- oauth2.0_token(facebook, myapp, scope=scope) fb_oauth <- sign_oauth2.0(facebook_token$access_token) if (GET("https://graph.facebook.com/me", config=fb_oauth)$status==200){ message("Authentication successful.") } } ## less early httr versions if (packageVersion('httr') > "0.2" & packageVersion('httr') <= "0.6.1"){ fb_oauth <- oauth2.0_token(facebook, myapp, scope=scope, cache=FALSE) if (GET("https://graph.facebook.com/me", config(token=fb_oauth))$status==200){ message("Authentication successful.") } } ## httr version from 0.6 to 1.1 if (packageVersion('httr') > "0.6.1" & packageVersion('httr') < "1.2"){ Sys.setenv("HTTR_SERVER_PORT" = "1410/") fb_oauth <- oauth2.0_token(facebook, myapp, scope=scope, cache=FALSE) if (GET("https://graph.facebook.com/me", config(token=fb_oauth))$status==200){ message("Authentication successful.") } } ## httr version after 1.2 if (packageVersion('httr') >= "1.2"){ fb_oauth <- oauth2.0_token(facebook, myapp, scope=scope, cache=FALSE) if (GET("https://graph.facebook.com/me", config(token=fb_oauth))$status==200){ message("Authentication successful.") } } ## identifying API version of token error <- tryCatch(callAPI('https://graph.facebook.com/pablobarbera', fb_oauth), error = function(e) e) if (inherits(error, 'error')){ class(fb_oauth)[4] <- 'v2' } if (!inherits(error, 'error')){ class(fb_oauth)[4] <- 'v1' } return(fb_oauth) }
Fix Credit goes to (www.listendata.com/) Visit this link for more information.
Thanks!
Happy Learning!
Follow @shobhitsinghIN
hi
this is my first time using R and FB application.
I’m trying to get all the pages liked by my friends, is it possible???
LikeLiked by 2 people
getLikes {Rfacebook package in R} in Extract list of likes of a Facebook friend. Please try this.
LikeLike
i tried this but i have to invite people to use my application and that’s what i don’t want to do , what i want to do is to get all the pages liked by friends and their categories,
LikeLiked by 1 person
Hello. Did you found a way to get the information without inviting friends to use your app?
LikeLike
i want to get a data from a site that uses facebook login, fbOauth should help me login to get this data?
LikeLike
Hi! I am interested in examining the personal public profile contents of a group (e.g., Group X) for posts relating to particular key terms (e.g. ‘a’ and/or ‘b’). I do not expect members of the group to post about ‘a’ and/or ‘b’ in the page of Group X so it would not help to simply look at posts within that group’s Facebook page. But I expect the members to be posting about ‘a’ and/or ‘b’ in their own personal public profiles which I hope I can examine and discuss in terms of the composition of Group X. Thanks for any help.
LikeLike
Hi i got by appid and app secret, but they said it could not load the package of “httpuv”, how could I solve the problem?
LikeLike
Can you please share what error is coming while loading the package “httpuv”
LikeLike
Hey, Is there a way to get data on events created on Facebook?
LikeLiked by 2 people
Yes, use search?q=beer&type=event
in Graph API
Refer:-
https://developers.facebook.com/docs/graph-api/using-graph-api/#fieldexpansion
LikeLike
Hi Shobit, A very good post! Initially I struggled to the Rfacebook package installed right but after disabling anti-virus software it all went fine! Thanks.
Manjunath
LikeLiked by 1 person
Thank you!
LikeLike
Is there anyway to get all the messages from a user?
LikeLike
i faced “authentication failed” problem. can you help me.
LikeLike
Shobhit,
Thanks for your efforts. Have a quick question. Lets say we need to find various events created in a specific location and people who are planning to attend. Do we need to have any friends attending this event or can we just use a developer account and search for that event like what you have mentioned in your earlier posts ?
I guess my question is can we mine Facebook data for Public pages without liking or disliking them , and do we need to have friends or acquaintances who may or may not be attending events or say liking a page or not ?
So for an example: Let say we want to find number of users who like the Public Page of say Samsung, or say Apple or LG ?
I hope I am clear in my question.
Thanks
J
LikeLiked by 1 person
Hi Shobhit
am trying to extract the number of views on a post in FB but have not been able to get the solution, do you have any idea
LikeLike
This is a goog post.I am facing error in the second step.The error is could not find function “fb0Auth” . Could you help with that.
LikeLiked by 1 person
nice bro
LikeLiked by 1 person
Hi. I’m trying use the function getLikes, but i receive the follow error: “User not found, or token is not authorized.”. Do you have any idea to solve this problem?
LikeLike
the same here
LikeLike
Very nice post. Keep it up.
LikeLiked by 1 person
Hey Shobhit! Thanks for the article – I’m a newb to R and am trying to authenticate with Facebook. Got to “Authentication complete and successful,” but when I try to save “my_oauth” I get the error: Error in save(my_OAuth, file = “my_oauth”) : object ‘my_OAuth’ not found.
Not sure what I’m doing wrong!
LikeLiked by 1 person
Never mind! The issue was that I needed to save “fb_oauth” in stead of “my_oauth”
LikeLiked by 1 person
Thanks for your feedback.
LikeLike
Great man
LikeLiked by 1 person
Please Help me to remove this error. why I am getting this error.
Waiting for authentication in browser…
Press Esc/Ctrl + C to abort
Authentication complete.
> save(fb_oauth, file=”fb_oauth”)
> load(“fb_oauth”)
> me <- getUsers("me",token=fb_oauth, private_info=TRUE)
Error in callAPI(query, token) :
An active access token must be used to query information about the current user.
LikeLiked by 1 person
I am getting the same error, is there a way to solve this issue ?
LikeLiked by 3 people
Thanks for your feedback. I have updated the fix in this blog.
LikeLike
I am getting the exact issue. I saw the my original authentication shows authentication complete. I believe it should be accompanied by Authentication Successful. but I didn’t see that. What about you?
LikeLiked by 1 person
Thanks for your feedback. I have updated the fix in this blog.
LikeLike
Thanks for your feedback. I have updated the fix in this blog.
LikeLike
Shobit
Excuse me but what was changed. I looked at the code. it looks the same to me.
LikeLiked by 1 person
Go to the bottom of this blog.
LikeLike
Hi, I am getting the same error. Help me
LikeLike
Do you have any idea how can we retrieve images people share or like of face book ?
LikeLike
Let me make a few things clear about the steps given in this blog, all of this can be mostly applied to only public pages on Facebook, like Newyork times, time of india etc. If you want to do it on your friends, then they would have had to set the posts as public before they share it, the same holds with photos and videos, in your case I believe there will be url links to the posts which the user has liked.
LikeLike
hello Shobhit! i have done all the steps successfully in console window without any error but my results are not shown in source window. after every command execute blank page is open on source window instead showing results…….
LikeLike
I tried to replicate the excercise with
searchPages<-searchPages("Sports", token, n=50) but it gave me an error
In vect[notnulls] <- unlist(lapply(lst, function(x) x[[field]])) :
number of items to replace is not a multiple of replacement length
I don't know what is the error.
Could you please help me?
LikeLike
by the way I already have the long live token from FB that’s because i put token instead of token=fb_oauth
LikeLike
HI ,
Really nice blog, Thanks fro sharing !
I have a question – Is there any possibility we can all public posts from facebook which contain a particular string ?
for example – all posts having string “LONDON” ..
LikeLike
Hi shobhit. Great article. I am getting an error in this, could you look upto it?
Copy and paste into Site URL on Facebook App Settings: http://localhost:1410/
When done, press any key to continue…
Waiting for authentication in browser…
Press Esc/Ctrl + C to abort
Authentication complete.
Error in curl::curl_fetch_memory(url, handle = handle) :
Problem with the SSL CA cert (path? access rights?)
Called from: curl::curl_fetch_memory(url, handle = handle)
LikeLike
hi this my first time that I am using the R and Facebook API.can we pull the profile data( birthday, About,contact details etc)through this.And also can we use google sheets to directly call the Facebook API to the sheets
LikeLike
Hi, great info.
Im a noob in R.
How would I use AND for strings? So I could search two or more terms for a page or group.
Or can this be done in R, so I do two separate searches and then compare for a match?
Thanks
LikeLiked by 1 person
amazing! it works really fine for me!, thanks for share…
LikeLiked by 1 person
it was super code and explanation sir……
LikeLiked by 1 person
where can i find the site URL area in Facebook after creating ID and App_Secret? Thanks
LikeLike
sorry, same query. where can i copy and paste to Site URL?
Copy and paste into Site URL on Facebook App Settings: http://localhost:1410/
LikeLike
The app setting (Basic) below Dashboard does not show any blanket/space for Site URL?
LikeLike
Can I get the comments in the post with this packages?
LikeLike
What an interesting article, exactly what i needed to get me started!
I am interested if one is able to extract all public profiles listed as living in an city or another, and how should this task be approached.
LikeLiked by 1 person
> my_oauth this shows up in Browser -> Authentication complete. Please close this page and return to R.)
And back to R…
Waiting for authentication in browser…
Press Esc/Ctrl + C to abort
Authentication complete.
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, :
Bad Request (HTTP 400).
How can I resolve this?
LikeLike
Sorry, typo
> my_oauth <- fbOAuth(app_id="ooo", app_secret="ooo")
Copy and paste into Site URL on Facebook App Settings: http://localhost:1410
When done, press any key to continue…
I press enter then that message shows up in broswer.
LikeLike
I have the same problem! I know this has been posted a while ago, but did you find a fix in the meantime? Or can someone else help me out?
Would be much appreciated!
LikeLike
Hello Shobhit, thanks much for this post and very detailed instructions. Have a question, if I have the Facebook is it possible to get the country for a Facebook profile given one has the profile url?
LikeLike
Is this code working now? I got errors in get likes function,
Error in getLikes(user = “me”, token = my_oauth) :
User not found, or token is not authorized.
Any solution for this? Is it because Facebook has changed the policies?
LikeLiked by 1 person
Hey Shobit, what if i want users info who have done check-in to a particular place ?
For example : Lets say I want the users data(like age,gender, location they are from) who have done the check-in Mumbai ?
If yes ,please guide me
LikeLike
I want to extract user’s data
LikeLike
Hi,
Thank you so much for this post. I would likw to ask if it is possible to extract facebook user profile data like name school attended and location of public accounts by searching school attended? Thank u
LikeLike
searchGroup(“DATAMININK”,fb_oauth)
No groups with this name were found.
data frame with 0 columns and 0 rows
what can i do
LikeLike
hi, I followed your code step by step but after getting me$name the code stoped working and I am just receiving errors. Can you take a look to my code and see where mistake is?
save(my_oauth, file=”my_oauth”)
load(“my_oauth”)
me <-getUsers("me", token = my_oauth)
me$name
#doesn't work
load('my_oauth')
my_likes <- getLikes(user = "me", token = fb_oauth)
view(my_likes)
getpagedata <- getPage("766974866669165", token = my_oauth, n=10)
#doesnt work
mygroup <- searchGroup(name = "DataMining", token = my_oauth, api = NULL)
group_post <- getGroup("5582633474", token = my_oauth, n=10,since = NULL, until = NULL )
search_pages<-searchPages("sports", token = my_oauth, n=10)
LikeLike
and errors look like this:
> my_likes view(my_likes)
Error in view(my_likes) : could not find function “view”
> my_likes view(my_likes)
Error in view(my_likes) : could not find function “view”
> my_likes <- getLikes(user = "me", token = fb_oauth)
Error in callAPI(query, token) : object 'fb_oauth' not found
LikeLike
Every weekend i used to pay a visit this web page, for the reason that i want enjoyment, for the
reason that this this site conations in fact pleasant funny
stuff too.
LikeLiked by 1 person
Thanks a lot. This is great content. I might share some for you guys too. Great work poster!
LikeLike
Hi Shobit,
Thanks a lot for this tutorial. However, one quick question. Let’s say I have a public Facebook page on movies which is ABCD for example. Now, I would like to extract only posts that contain a specific keyword “Spielberg”. Now the thing is that page might have been active for the past 8-10 years. So, how can I extract the posts (which contain “Spielberg”) from the creation of page date?
Meaning I want all the posts (which contain Spielberg) from the page?
LikeLike