Analyze Facebook with R

Hello everybody!
Today I found something very cool: There is a R package for mining Facebook.
For Twitter there are different, but this is the first one really working well with Facebook.
So I wanted to test it and was amazed about how easy it works.

Setup:

The first thing we need is a Facebook app.
Visit the Facebook developer center, register as Developer and create your test app.
So visit https://developers.facebook.com/apps and click „register as developer“.

You can then create your new app:

df5753d952ab36320531efdda1d57bf4

You can name it however you want as we won´t make this app available for all Facebook users.
And that´s it!
We created our Facebook App.

Bildschirmfoto 2013-11-19 um 22.21.38

Just keep this tab open and let´s look at R.

R

Authentication

WARNING: There seems to be a problem with doing the authentication process in RStudio. A possible solution is to authenticate and create the oAuth token in a normal R session and then load it in RStudio to continue.

First we need to install the packages Rfacebook and Rook
It is important to write it exactly this way as there is an older package with nearly the same name, but it won´t work.

cd4ef1dbf77a43117054ba8865c4ace7 1185f3caaf97d7b975caf386c31c9582

Ok now we need to connect our R session with our test app and authenticate it to our Facebook Profile for Data Mining.
Rfacebook offers a very easy function for that.
Note: I use an app for the authentication. You can also you OAuth tokens, but they will expire after 2 hours and you have to get a new one.

fbAuth


fbOAuth(app_id, app_secret, extended_permissions = TRUE)

So it just needs the app-id and the app-secret. But let´s try it:


 require("Rfacebook")
 fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D")

Of course you have to insert the app-id and the app-secret of your app you just created

Bildschirmfoto 2013-11-19 um 23.41.18

The function will return the following:

83cc56c80c26ecc29fa16a3263cd67b8

Here you can see how the Rfacebook package creates the connection to the Facebook API. It creates a listener on the port 1410.
And now we have to tell our app to send the data we want to this port.
Go to your app in the Facebook Developer center and go to Settings / Basic.

d96e511e0922ba99c3fec5b516f171dd

Then click on „Website with Facebook Login“ and enter http://localhost:1410/

0dcd43b71b6b94258329cd60e5d30a20

Then save the changes and go back to your R session to hit any key to tell R that we can go on.

Your browser will then pop up and the app will ask for your permission

31bae8ef43f41955117d0da1e59dbc3f e58269bb0918ef2ebe1d6b20d4e5c0c5

Then wait a few seconds and the website will return:

5fcd62d2245a33c93e4085b0b8a5af1c

And that´s what we do.

075dcaace9d9b9f02558bbd11115405d


#now we have our fb_oauth connection
 #we will just save them to be able to use them later
 save(fb_oauth, file="fb_oauth“)

 #so if you want to connect to Facebook again you just have to call
 load("fb_oauth")

Get Data!

Now we connected everything and have access to Facebook.
We will start with getting our own profile information.

 #the getUsers function return public information about one or more Facebook user
 me <- getUsers("me", token=fb_oauth)
 

Now we saved our own public information in the variable „me“
You can test it for example with:

0dc0e4ddfe91b839fa690314290bc3a4

For the case you forgot your name ;)

Let´s take a look at our friends.
Rfacebook offers the function


 getFriends(token, simplify = FALSE)
 

so you get your friends with:

 my_friends <- getFriends(token=fb_oauth, simplify=TRUE)

Your friends list is ordered by ID, so you can see who of your friends was the first on Facebook. Some very interesting inside.

You can show the friends which are the longest time on Facebook with:

 head(my_friends, n=10)

But let´s go a step further and get some deeper information about our friends.

We now have the variable my_friends.
We can combine it with the

 getUser()

function for example to get the relationship status of our friends.

 my_friends_info <- getUsers(my_friends$id, token=fb_oauth, private_info=TRUE)

 #create a table with the relationship statuses

table(my_friends_info$relationship_status)

d13beec3b80973ce409a8ebbb268d0c7

Questions?

If you have any questions feel free to ask me on Twitter or write a comment here.

About these ads

69 thoughts on “Analyze Facebook with R

  1. I’m not sure why, but I followed these steps and could not get the OAuth to work properly.
    The developer settings seem right, I authenticate through facebook, it says that I’m authenticated, and fbOAuth continues on.
    My auth object is just a list with a function in it, and when I use it as a token, it doesn’t work (Error in callAPI(query, token) :
    An active access token must be used to query information about the current user)

    Using the 2 hr token works fine though. Any ideas?

  2. Pingback: Retreiving and analyzing Facebook data with R « Matteo Redaelli

    • Hey David,
      I noticed that there seems to be a small problem during the authentication process, when Facebook tries to send the answer the OAuth details to the local web server. Did your web browser say: “Authentication complete — you can now close this page and return to R” ? When you approved the app to access your profile

  3. I’ve same problem as David. In my browser tab there is “Authentication complete — you can now close this page and return to R”. What I saw is that in my R console there is “Authentication complete” but there ins’t “Authentication successful”. I’dont know why, but any querry I do doesn’t work

    • Hey sms1962,
      The problem seems to occur if you press the “Any Key” too fast. You should wait a few minutes as the servers need some time.
      I think this could be also your problem.
      I´ll check it and put a note in my tutorial

      I hope i could help you.

      Regards
      Julian

  4. Yes, I do get the message “Authentication complete — you can now close this page and return to R”, but the token still seems like it doesn’t work.

    I used the command fb_oauth <- fbOAuth(app_id="app_id", app_secret="app_secret") to generate the token (which appeared to work fine), but in your post you said "I use an app for the authentication" — perhaps that's the difference?

  5. Hmm, I just tried it again and it seems to be working this time. I did leave a little bit of extra time before the “press any key” step (there was a message about it taking several minutes for the change to propagate to servers). Perhaps that was the difference.

    • Hey David
      It´s good that it is working for you now but that´s an important aspect as i heard that there are several persons with this problem. I´ll place a note in my tutorial.
      Thanks for your help David.

      Regards
      Julian

  6. sure did (FF 25.0) and my R (3.02) just froze. For twitteR FF and R work fine but R studio chokes. The oauth seems to be the issue in both cases.

  7. Pingback: Social Circle Visualization | Amy Hanlon

  8. @julianhi Thx for you help. In my case, the problem was with R Studio, as @Tim write in comments. In R everything works as a charme, so I create fb_oauth in R and then use it with no problem in R Studio. IMO you should put info about this in your article.

  9. I can reliably replicate the problems folks have had with the latest (preview) version of RStudio. The oauth simply is not completing. It gives the “completed” but not the “successful” message. Running the auth token generator outside R studio worked just fine, and then you just proceed in RStudio as normal, loading the token from a file.

  10. After the fb_oauth <- fbOAuth(app_id="", app_secret="“) step, I see "Waiting for authentication in browser…" in the normal R session (not Rstudio) but it doesn't open any browser or do anything. When I run this in Rstudio, it does open up a tab in my default browser. Is there something I'm missing in my normal R session setup?

    • Hey Kan SJ
      sorry for my late answer.
      Do you use windows or mac?
      It is possible that the instance of the R framework, does not have to permission to open a browser windows.

      Regards

  11. Hey,
    Thanks! great article :)

    I search in help but I can’t find answer for my question:

    Is it possible to anlalize with this package fun_pages that uses clicks as “liked”?

    Once I used for this FQL:

    SELECT page_id,name, page_url
    FROM page
    WHERE page_id IN (
    SELECT page_id
    FROM page_fan
    WHERE uid IN
    (SELECT uid2 FROM friend WHERE uid1 = me()))

    thnx for any help with this :)

  12. Hi Julianhi

    Thank for your tutorial. Really great and worked for me. Do you have, lets say guide book or anything exploring the FB API with R? Definitely it will be having possibilities to make great work. Thank in advance.
    Rgds

  13. Hi julianhi

    Thank much. I got that. I’ve also tried some of the scripts in the tutorial. I wonder, can I retrieve or plot friend network? Using getNetwork I can plot my network but friends of mine?

    • Hi ided,
      it is not possible directly. Your friends would have to give access to the App and allow it to download their friends list. But for this you would have to do a lot of changes on the app.
      I hope i could help you.

      Regards

  14. Hi

    Ever time i try to authenticate by pasting site URL in Facebook, it gives an error as “something is wrong, we are working on it”

    Any help or comment on this type of error.

    Regards
    Pankaj

    • Hey Pankaj,
      I have this problem as well and also talked to somebody else who had this problem today. This seems to be caused by Facebook. There seems to be something wrong with their server. I will look if I can find a solution and will let you know.
      Regards

  15. Hi Julian,

    I have a “small” problem, I was wondering if you had any solution: I cannot create the app. Facebook says you need to be a verified developer. Point is, to be one you have to a) confirm your phone number or b) add your credit card details… And I have already done both. I also opened a ticket on Facebook about it: https://www.facebook.com/help/community/question/?id=10203002345927583&added and one on Stack Exchange: http://webapps.stackexchange.com/questions/54662/i-cannot-create-facebook-apps both with no luck. Any idea?

    • Hey Alessandro,
      there seem to be some heavy problems in the Facebook App Developer Center. I heard from a lot of people that they have problems. For example when they try to save the App an error message appears. I think Facebook is working on the Center and is doing some updates. I couldn´t think of another reason for all these problems.
      But I will follow your problem and hope that I can find a solution.

      Regards

      • Thank you very much, Julian. Much appreciated.

        I think you’re right, apparently I am not the only one experiencing some problems. Yet, one would expect slightly more support/information from a company such as Facebook. Please, keep me updated if you find any solution.

  16. Hi Julian, great blog you have here man!. I have 2 q’s:

    1) Using OAuth to connect with facebook, using pablobarbera / Rfacebook package to read facebook.
    the following example is extracted from the ?getPost doesnt work:

    #R Code
    fb_page <- getPage(page="facebook", token=fb_oauth)
    post n.l || n.comments > n.c) { :
    # missing value where TRUE/FALSE needed

    2) Im trying to read information on a fanpage (from someone else), you know friends list, likes. I saw someone asked for this, would you know how could i gain permission from that fanpage’s owner? I mean, he could actually give me administration rights, but still how could i tell GraphApi to query it, is there any “Add user” or “Add Page” to ask on GraphApi or App Configuration (OAuth) where i could achieve this?

    Just by answering any of the 2 q’s would be of high relevance for us

    • Hey Rodrigo,
      sorry for the late answer and thanks for your comment.
      1.) As far as I know you have to set a value for n in the getPage() function. Pablo suggests to set this number very high to make sure to get every single post. It should look like getPage(“facebook”, token, n=8000).
      Does this work for you?
      2.)You don´t really need this kind of permission. The getPage() function gives you access to the Page likes for example even if you are not the admin of the page. What information do you need from the fanpage?

      I hope I could help you a little bit.
      Regards

  17. Hi Julian,

    Your blog is very helpful for sophomores like me but I came across this problem :

    After giving this command. I get authentication complete but just after that I get the below mentioned error

    > fb_oauth <- fbOAuth(app_id="*********************", app_secret="**********************")

    Copy and paste into Site URL on Facebook App Settings: http://localhost:1410/
    When done, press any key to continue…
    starting httpd help server … done
    Waiting for authentication in browser…
    Authentication complete.
    Error in function (type, msg, asError = TRUE) : couldn't connect to host

    How should I rectify the above error ?

  18. <- getUsers("me", token=fb_oauth)
    Error in callAPI(query, token) :
    An active access token must be used to query information about the current user.

    Why am I getting this error ? I try many times. but i didn't solve it. please help me out

  19. Hi Julianhi,

    I tried to follow your description, but when I come to the part were you do the authentication. I don’t get the message that the authentication was successfull.

    I only get the following:

    > require(‘Rfacebook’)
    > fb_oauth <- fbOAuth(app_id=xxxxxxxxxxxxxxxxxxxxxxxx, app_secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
    Copy and paste into Site URL on Facebook App Settings: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    When done, press any key to continue…
    Loading required package: Rook
    Loading required package: tools
    Loading required package: brew
    Waiting for authentication in browser…
    Authentication complete.

    I get the messaga in my brower that the authentication was completed and that I can return to R. But there it stops.

    Can you please help me out?
    I tried it several times, but I don't see what I'm doing wrong.

    Thanks in advance!

    K regards,

    Debbie

    • Hey Debbie,
      so when you do auth step does there appear the browser window or is nothing happening?
      If the browser window appears with the URL in the URL bar and the page just doesn´t load, you normally just have to copy&paste the URL and hit enter again.

      Regards

  20. thanks for your reply. and here is my code
    install.packages(“Rook”)
    install.packages(“Rfacebook”)

    require(c(“Rook”,”Rfacebook”))
    fb_oauth <- fbOAuth(app_id="176988012511120", app_secret="991efd8aba85c69235c4431b57dc12b8")

    save(fb_oauth, file="fb_oauth")
    load("fb_oauth2")
    me <- getUsers("me", token=fb_oauth)

    about 15 days ago i did it successfully. but after changing my fb password i lost my previous authentication. I open another apps. But this time I cant complete my steps. why i dont know. plz help me out.

    Note: and i have another little problem, i couldnt find the same page you mentioned in your screenshot(basic setting).
    thanks in advance

    • Hey Akilesh,
      there is an easy answer: I think you are querying too many users.
      I think the rate limit is something like 100 users at once. If you want to query more users you should
      do it in several steps.
      Please let me know if this could solve your problem.

      Regards

  21. Hi Julian
    Thanks for sharing this package.
    I’m having a problem to fetch comments from posts of a page.In the code I’m fetching a page and storing the details in fb_page1 and trying to fetch posts from that page(fb_page1). Further I need to fetch the comments for these posts which are fetched in newpost. I’m able to fetch the comments but in some cases
    1) where there are nested comments (like comments for a comment) the $comments_count value from the post is incorrect and I’m not able to fetch any value. The message is (data frame with 0 columns and 0 rows)
    2)It is considering the nested comments as a comment in $comments_count but in some cases it is not. So should we consider nested comments or ignore them.
    3) and i have observed that the comments are fetched only if the $comments_count is greater then 2 or 3 otherwise it gives a message
    (data frame with 0 columns and 0 rows).
    This is my code
    fb_page1 <- getPage(page="clubmahindra", token=fb_oauth)
    for(i in 1:nrow(fb_page1))
    {

    if(fb_page1$comments_count[i]!='0')
    {
    postid[length(postid)+1]<-fb_page1[i,7]
    post <- getPost(post=fb_page1[i,7], n=100, token=fb_oauth)
    newpost<-do.call(rbind.data.frame, post[3])
    }
    }
    This is what i observed might be wrong
    Regards

    • Hey Onkar Khaladkar,
      It seems like there is some inconsistency. But I don´t know if it is caused by the Facebook API or by the wrapper of the Rfacebook package.
      I definitely will have a look at it and will also ask the developer of the package.
      I´ll reach out to you as soon I have some more details.

      Regards

  22. Hi guys. I have problem/error when using command me <- getUsers("me", token=fb_oauth ): Error in callAPI(query, token) :
    An active access token must be used to query information about the current user.
    Where I can find solution of this problem?

    Full program code:

    require("Rfacebook")
    require("Rook")
    fb_oauth <-fbOAuth(app_id="235680489948000", app_secret="d3e67a6ad8dfba19fb9191d409de3123")
    save(fb_oauth, file="fb_oauth");
    load("fb_oauth");
    me <- getUsers("me", token=fb_oauth )
    my_friends <- getFriends(token=fb_oauth)

  23. Hi,

    I am facing problem with authentication. After it says to press key:

    I am having error:

    Error in authorizer(authorize_url) :
    httpuv package required to capture OAuth credentials.

    The settings are fine on app. I am using windows and fireforx 27.0.1

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