Download your Facebook photos using R

September 15, 2012

(This article was first published on Stats raving mad » R, and kindly contributed to R-bloggers)

So tonight I wanted to download all my Facebook pictures. For some reason the zip file was corrupted each of the 3 times I downloaded, so I remembered that some time ago I was playing around with an R project named Facebook Data-Mining. The project is conveniently located at github and you can access it here. Looking at the  project description you realize that you can do much more with the project.

First of all you need an Access Token that you can get from After you get it navigate to the AccessToken.R file.Copy and paste the following as I added here the Facebook ID definition

# Get a Facebook Graph API Explorer Access Token
# Go to '', 
# login and click "Get access token"
# Your ID is located first in the output of the Graph Explorer,it's all numbers
# Store your access token here:

me <- "PasteYourFacebookIDHere"
access.token <- "PasteYourAccessTokenHere"

 Now go and source the Setup.R script

# Step 1: Create functions used by the demo
# This creates the 'facebook' function as described at
# and other functions to create initials, etc.
cat("Step 1: Create functions used by the demo","\n")

# Step 2: Run the requirements script
# This installs (if required) and loads the mandatory libaries
cat("Step 2: Run the requirements script","\n")

Finally, run the following excerpt from the Photos.R script

# Create a "photos" directory (Warnings ignored so if it 
# already exists it will safely continue)

# Create a "photos" directory or call it as you like it
dir.create( "photos", showWarnings = FALSE )

# Create a directory for the individual's photos
dir.create( paste("photos",, sep="/"), showWarnings = FALSE )

# Download each of the individuals photos
for (i in 1:length( {

Then R will output a bunch of download streams, like this and inside the working directory you will find a photos folder and a Facebook subfolder

Note : It is likely that you will get the known error when a connection to Facebook via R is made

Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The solutions to this are outlined in the A tiny RCurl headache post.

To leave a comment for the author, please follow the link and comment on his blog: Stats raving mad » R. offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.