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!

Advertisements

33 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,

        Like

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

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

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

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

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s