
Authentication

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

1 2 3 4 |
app_name <- "ThinkToStartTest" client_id <- "XXX" client_secret <- "XXX" scope = "public_content" |
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) |
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] |
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.
1 |
user_info <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/self/?access_token=',token,sep=""))) |
1 |
received_profile <- user_info$data$id |
Analyze Instagram with R
1 |
media <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/self/media/recent/?access_token=',token,sep=""))) |
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
1 2 3 |
require(rCharts) m1 <- mPlot(x = "date", y = c("likes", "comments"), type = "Line", data = df) |