This tutorial will show you how you create an Instagram app, create an authentication process with R and get data via the Instagram API.

There is no R package for this yet so we have to configure the authentication and data download process on our own. But Instagram offers a pretty good documented API and uses oAuth 2 which makes it easy to use with R and the httr package for example.

Authentication

The place to start for everybody who wants to work with the Instagram API is http://instagram.com/developer/

Here you can find all the information you need and also manage your apps.

So click on „Register Your Application“ and go through the login.

On the next screen you can set the parameters for your app. Choose an application name ,write a small description of what your app will be about and add a webiste

Then you have to enter an OAuth redirect URI. To choose it go to your R console and execute following code:

require(httr) full_url <- oauth_callback() full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\\1") print(full_url) 1 2 3 4 require ( httr ) full_url < - oauth_callback ( ) full_url < - gsub ( "(.*localhost:[0-9]{1,5}/).*" , x = full_url , replacement = "\\1" ) print ( full_url )

This will show you the preferred callback URI for httr. Copy this URL and paste it in your app settings.

This is how my settings look like:

After clicking on „Register“ you will be redirected your app authentication details we will need for our analysis.

In R we have to define 4 variables:

app_name <- "ThinkToStartTest" client_id <- "XXX" client_secret <- "XXX" scope = "public_content" 1 2 3 4 app_name < - "ThinkToStartTest" client_id < - "XXX" client_secret < - "XXX" scope = "public_content"

The first 3 you get from your app settings. The third on scope is basically the level of authorization you want to get. Basic is enough to download data like likes or comments. If you actually want to post something to Instagram you need another scope. You can find more information on the Instagram developer page about that.

Then we create our Instagram in R for the httr package. This is the app we will use to connect to the API. To do so we have to provide the access points.

instagram <- oauth_endpoint( authorize = "https://api.instagram.com/oauth/authorize", access = "https://api.instagram.com/oauth/access_token") myapp <- oauth_app(app_name, client_id, client_secret) 1 2 3 4 instagram < - oauth_endpoint ( authorize = "https://api.instagram.com/oauth/authorize" , access = "https://api.instagram.com/oauth/access_token" ) myapp < - oauth_app ( app_name , client_id , client_secret )

In the next step we do the authentication

ig_oauth <- oauth2.0_token(instagram, myapp,scope="basic", type = "application/x-www-form-urlencoded",cache=FALSE) tmp <- strsplit(toString(names(ig_oauth$credentials)), '"') token <- tmp[[1]][30] 1 2 3 ig_oauth < - oauth2 . 0_token ( instagram , myapp , scope = "basic" , type = "application/x-www-form-urlencoded" , cache = FALSE ) tmp < - strsplit ( toString ( names ( ig_oauth $ credentials ) ) , '"' ) token < - tmp [ [ 1 ] ] [ 30 ]

Now your browser should open and ask you to give permission to the app. After you returned to R you should have received your access token.

Our analysis focuses on analysing our own profile. Instagram is very restrictive when it comes to sandbox apps and they don´t return any other data than the data from your own profile.

user_info <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/self/?access_token=',token,sep=""))) 1 user_info < - fromJSON ( getURL ( paste ( 'https://api.instagram.com/v1/users/self/?access_token=' , token , sep = "" ) ) )

received_profile <- user_info$data$id 1 received_profile < - user_info $ data $ id

Analyze Instagram with R

We can now download your most recent media items from Instagram.

media <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/self/media/recent/?access_token=',token,sep=""))) 1 media < - fromJSON ( getURL ( paste ( 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' , token , sep = "" ) ) )

This returns the recent 20 pictures of the user we will use for our analysis. We go through them with a for loop to extract the count of likes and comments and the date and time the photo was posted.

Instagram uses UNIX timestamps as their date. So we have to convert it to make it readable.

df = data.frame(no = 1:length(media$data)) for(i in 1:length(media$data)) { #comments df$comments[i] <-media$data[[i]]$comments$count #likes: df$likes[i] <- media$data[[i]]$likes$count #date df$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01")) } 1 2 3 4 5 6 7 8 9 10 11 12 13 df = data . frame ( no = 1 : length ( media $ data ) ) for ( i in 1 : length ( media $ data ) ) { #comments df $ comments [ i ] < - media $ data [ [ i ] ] $ comments $ count #likes: df $ likes [ i ] < - media $ data [ [ i ] ] $ likes $ count #date df $ date [ i ] < - toString ( as . POSIXct ( as . numeric ( media $ data [ [ i ] ] $ created_time ) , origin = "1970-01-01" ) ) }

Visualization

Now we can visualize the data. I will use the rCharts package to so. Of course you can also use ggplot2 or whatever package you like.