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 fb-create-new-app-button button near the top. Once you are done with the verification process, your application is created. Note down the App Id & App Secret.

1
Application Dashboard

Site URL on Facebook App Settings: http://localhost:1410 

6.PNG
App Settings

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”)

4

5

Saving “my_oauth”as a file to be re-used in future sessions, which can be used as token in functions.

7.PNG

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.

8.PNG

Here is the my result data set “my_likes”. With three variables: Id, names & website of pages.

view.PNG

9.PNG
Pages Liked

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.

output_LJA4Du.gif

After getting the page id use below command in R to get posts.

getpage.PNG

Here is my result data set “getpagedata”. With 10 Observation & 10 Variables. Variables like Post with likes_count, share_count & comments_count etc.

padess.PNG

1a

1b


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.

groups.PNG

Here is my result data set “search_groups”. With many observation & 3 Variables.

11.png
Groups


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.

12

Here is my result data set. With 10 Observation & 3 Variables.

postttttttttt

12a


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.

sports.PNG

Here is my result data set with 10 Observation & 16 Variables.

s1

s2


function updateStatus

updateStatus(“text”, token)

Arguments: text: any string , token

Use below command in R to update Facebook status.

status1.PNG

Result:

status2


 

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)
}

111

script.png

Fix Credit goes to (www.listendata.com/) Visit this link for more information.


Thanks!

Happy Learning!

64 thoughts on “Mining Facebook Data Using R & Facebook API!

      1. 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,

        Liked by 1 person

  1. i want to get a data from a site that uses facebook login, fbOauth should help me login to get this data?

    Like

  2. 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.

    Like

  3. 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

    Liked by 1 person

  4. 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

    Like

  5. 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?

    Like

  6. 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!

    Liked by 1 person

  7. 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.

    Liked by 1 person

    1. 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?

      Liked by 1 person

    1. 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.

      Like

  8. 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…….

    Like

  9. 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?

    Like

  10. 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” ..

    Like

  11. 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)

    Like

  12. 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

    Like

  13. 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

    Liked by 1 person

  14. 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.

    Liked by 1 person

  15. > 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?

    Like

    1. 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!

      Like

  16. 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?

    Like

    1. 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?

      Liked by 1 person

  17. 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

    Like

  18. 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

    Like

  19. 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)

    Like

  20. 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

    Like

  21. 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?

    Like

Leave a comment