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.


First we have to get an access_token for our connection. To get it go to

As API version use “Unversioned” and then click on “Get Access Token”.access token facebook open graph r

The easiest way is to select all permissions.

access token dialog R facebook

Then click on “Get Access Token” again and a dialog will appear which asks you to agree that the token can get all the information you just selected.

facebook dialog R

Click on “Okay” and you can find your Access Token in the box.



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: We are using a temporary access token in this example. You can also you authentication with an app, but there seems to be a problem with the Rfacebook package. I will add it again when it is working again.

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. If we use “me” as the username argument, it will return our own profile info.

me <- getUsers("me",token=token)

Now we saved our own public information in the variable „me“

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, simplify = FALSE)

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 getUsers() function.

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

For example to get the relationship status of our friends.





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

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


  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.


  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.


  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.

  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.


  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.


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

  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: and one on Stack Exchange: 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.


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

  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,


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


  20. thanks for your reply. and here is my code

    fb_oauth <- fbOAuth(app_id="176988012511120", app_secret="991efd8aba85c69235c4431b57dc12b8")

    save(fb_oauth, file="fb_oauth")
    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.


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

    post <- getPost(post=fb_page1[i,7], n=100, token=fb_oauth)
    newpost<, post[3])
    This is what i observed might be wrong

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


  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:

    fb_oauth <-fbOAuth(app_id="235680489948000", app_secret="d3e67a6ad8dfba19fb9191d409de3123")
    save(fb_oauth, file="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

  24. my_friends <- getFriends(fb_oauth,simplify=TRUE)
    Error in matrix(unlist(content$data), ncol = 2, byrow = TRUE) :
    'data' must be of a vector type, was 'NULL'. Please help me in resolving this issue

  25. Hi Julian
    Thanks for sharing this article.
    I’m having a problem to call getUsers(“me”,token=fb_oauth). How to fix it?
    Thanks a lot!

    > fb_oauth save(fb_oauth, file=”fb_oauth”)
    > load(“fb_oauth”)
    > me <- getUsers("me",token=fb_oauth)
    Error in callAPI(query, token) :
    (#12) username is deprecated for versions v2.0 and higher

  26. I got the same problem with function: > me <- getUsers("me", token=fb_oauth, private_info=FALSE)
    Error in callAPI(query, token) :
    (#12) username is deprecated for versions v2.0 and higher

    any idea???? thx in advance

  27. Hi Julinahi , yes I got the authentication message and one interesting thing I am getting this error only when I am using my own api . I have named the api as Naveen if I select GraphAPI I am not getting this error and when I try to extract the friends list I am getting the list of only 3 friends but I have the friends list of more than 400.

    • Hey Naveen,
      Facebook made a big change to it´s API what causes a lot of errors when you execute this code and makes it not usable anymore. As soon as I can find a solution I will update the code.
      Follow me on Twitter or add my Blog to stay up-to-date.


  28. hey, Same error :

    got the same problem with function: > me <- getUsers("me", token=fb_oauth, private_info=FALSE)
    Error in callAPI(query, token) :
    (#12) username is deprecated for versions v2.0 and higher

    • Hey EQD,
      Facebook made a big change to it´s API what causes a lot of errors when you execute this code and makes it not usable anymore. As soon as I can find a solution I will update the code.
      Follow me on Twitter or add my Blog to stay up-to-date.


  29. Hi .. many many thnx for the nice post:)
    How ever I am getting the following error while using the function
    > me <- getUsers("me", token=fb_oauth, private_info=FALSE)
    Error in callAPI(query, token) :
    (#12) username is deprecated for versions v2.0 and higher

    Can anyone suggest somethings on this

    • Hey Briti,
      Facebook made a big change to it´s API what causes a lot of errors when you execute this code and makes it not usable anymore. As soon as I can find a solution I will update the code.


    • Hey Briti,
      thanks for your suggestion. But it is not about directly connecting to Facebook or getting the data. It is more about the logical structure of the Facebook API which makes it hard. Facebook deprecated a lot of feature like the one you are using in your example. It is from version 1 but the Facebook API version is now 2. This works when you use the Graph API directly. But to make it easier the Rfacebook package uses the connection to a Facebook app. And all new created apps use the API version 2 now.
      But I think in a few days there will be an update to the Rfacebook package and I will update my post.


  30. Hi Julianhi,

    I am an beginner for R and Facebook API, and thanks for your article, which is very detail and helpful. It makes me have a progress on learning R and Facebook API.

    I am lost somewhere…
    When I do the action – fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D"), it connect to the browser and shows the error message-
    "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains."

    I try to get answer from internet, but failed. Could you give me some clues?

    Thanks a lot,


    • Hey,
      I´m sorry but Facebook made a big change to the API and so the tutorial does not work at the moment. I´m trying to find a solution at the moment.
      Please follow me on Twitter to stay up-to-date.


  31. i am not getting any friend list when i used the command my_friends <- getFriends(token, simplify=FALSE) . And I am also facing call API error. if it is resolved please tell me how to do it.

  32. yes i have followed the steps. and yes i have successfully authenticated. but when i used the command me <- getUsers("me",token=token). i got the error callAPI(query, token):username is deprecated for versions v2.0 and higher
    and after this I tried my_friends <- getFriends(token, simplify = FALSE) . i think after running this command i must get list of my friends but i am not getting it.

    • Yes you don´t get the list of friends because the first command where you get your own profile information was not successful. Did you set the API version to “Unversioned” before you created the access token?


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s