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:
First we have to get an access_token for our connection. To get it go to https://developers.facebook.com/tools/explorer
As API version use “Unversioned” and then click on “Get Access Token”.
The easiest way is to select all permissions.
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.
Click on “Okay” and you can find your Access Token in the box.
R
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.
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.
table(my_friends_info$relationship_status)
Questions?
If you have any questions feel free to ask me on Twitter or write a comment here.
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?
Hey Stephen,
I don´t really know why it doesn´t work for you. I´m new to the Rfacebook package as well. But I´ll take a look at it
Pingback: Retreiving and analyzing Facebook data with R « Matteo Redaelli
Nice! thanks
I’m having the same issues as Stephen — my token always generates the error “An active access token must be used to query information about the current user”.
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
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
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?
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
I had to change my primary browser to IE (from Firefox) and then everything worked fine
Hey Tim
im glad it is now working for you.
But at your first tries: did you use your primary browser to connect with the app?
Regards
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.
Pingback: Social Circle Visualization | Amy Hanlon
@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.
Thanks for your info.
I´ll put a hint in my tutorial.
I didn´t test it with RStudio as i´m not a real fan of it 😉
Regards
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.
Hey Mark,
thanks for your comment. I just added a hint in my tutorial.
Regards
Julian
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
fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D“)
app_secret="1A2B3C4D“)
correct code mark "1A2B3C4D") not “
Hey Salamon Aska,
thanks for your hint. I changed it in my tutorial.
Regards
Great post, thank you very much (I love step-by-step explanations!). Just added your blog to my RSS. 😉
Thank you Alessandro! I really like to hear that! It keeps me motivated!
Regards
Julian
is it any possible famous( it is public page) facebook user extraction using R tools
Hey satya,
take a look at the following tutorial and scroll down to “Analyzing data from a Facebook page”. This should dp everything you want.
Regards
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 🙂
Hey Rafal,
so you want to analyze the likes of a Facebook pages? Did i get you right?
You get do this with the getPage() function of the Rfacebook package as described in the documentation: http://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf
Regards
Hi Juiianhi
But how to get data about another user’s like?
Hi ided,
take a look here: http://stackoverflow.com/questions/9007976/get-all-friends-and-their-likes-using-the-facebook-api
This should be the function you are looking for.
I hope i could help you.
Regards
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
Hey idedided,
at the moment you can´t use all API calls with the Rfacebook package. You can see here what calls are supported at the moment: http://cran.r-project.org/web/packages/Rfacebook/Rfacebook.pdf
But i think a lot of calls will be added in the future. I hope i could help you.
Regards
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
Thank you very much Juliahi
That means I need friend’s API for doing that as mine?
Have a great new year
Thanks
You need your friend to grand access to the application. So give the application, you created, the right to download the private information.
Regards
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
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.
Hey Alessandro,
everything should work fine now. I can edit the settings again. How about you?
Regards
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
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 ?
Hey Aanchal,
Do you have some kind of firewall activated? Maybe you should turn it off and try again.
Regards
<- 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
Hey Nahid,
could you please post the whole code you are using? It seems like there goes sth wrong when you get you auth token. It is just not valid anymore.
Regards
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
Thankyou very much Julian ! I did have firewall activated. Its working fine now 🙂
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 !
I just solved my problems….
thanks and great post as well.
Hey Nahid,
sorry for the late answer. How did you solve the problem?
Regards
Hi Julian. Great article!
When I use the getUsers() function, I get this: “An unknown error occurred. Querying too many users?”
Any idea what I can do about that? Thanks.
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
This is really nice… just tried and and working fine
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
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)
Hey Saulius,
did you receive
Authentication complete.
Authentication successful.
After calling the fb_oauth function?
Regards
Hello Julianhi,
Thanks for trying to help me.
There is all log file of my actions: https://copy.com/G4hT3J6f3y8kVn1K
Any suggestion? Thanks.
Hey Saulius,
there seems to be a problem with the authentication when you have to allow the access in the web browser. The R session should return
Authentication complete.
AND
Authentication successful.
The last one is missing.
Please try to create a new Facebook app and connect to the new one. Seems like there are some settings cached.
Regards
Updated R from 3.0.0 to 3.0.3, but result are same (without warning)…
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
Hey Munish,
please try:
install.packages(“httpuv”)
before you run the code.
Regards
Oh Thanks, It was written just there. I am new to R.
After it says authentication completed. It throws error: Error: Deprecated: supply token object to config directly
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
Hey Naveen,
did you receive “Authentication successful” after the authentication process?
Regards
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
Hey,
do you use the newest version of the Rfacebook package?
Please check it with
update.packages(“Rfacebook”)
Does this work?
Regards
Hi,
I use the newest version of the Rfacebook pkg and it doesn’t work.
Would you please run your example without error, now?
(https://developers.facebook.com/docs/apps/changelog, => /me/username is no longer available.)
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
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.
Regards
Julian
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.
Regards
Julian
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.
Regards
Julian
Hello Julianhi;
I have find a alternative for that. Instead of me one has to use separate url now because facebook has changed the setting.
path=”v1.0/me”
access_toke=”….”
url <- sprintf( "https://graph.facebook.com/%s?access_token=%s", path, access_toke)
data <- getURL( url, ssl.verifypeer = FALSE)
my_profile=fromJSON( data )
This will work !!:)
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.
Regards
Hey Julian,
Any news about the problem?
Hey Lousie,
unfortunately there are no new news. But I will follow this topic and try to find a solution as soon as possible.
Regards
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,
Ning
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.
Regards
Thanks for your reply.
And Congratulation that Germany have beaten Algeria.
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.
Could you please provide some more details? What error message do you get? Did you follow all the steps in the authentication process?
Regards
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?
Regards
after changing it error is not appearing anymore. but output is not coming. Do i need to authenticate again?
What do you mean with authenticate again?
You just have to go to https://developers.facebook.com/tools/explorer , set API Version to Unversioned and click on Get Access Token.
Then you copy-paste this access token and save it in the variable “token”.
Did you follow the steps in this order?
Regards