Chapter 14 Instagram Basic Display API
Initially released in 2010, Instagram currently counts 1+ billion monthly active users with 50+ billion photos stored (Tankovska (n.d.)). User engagement is high, with an average of 28 minutes per day spent on the platform in 2020 (Aslam (2021)). Needless to say, the photo and video sharing social networking service holds invaluable data that could be leveraged by social researchers.
You will need to install the following packages for this chapter (run the code):
# install.packages('pacman')
library(pacman)
p_load('httr')
14.1 Provided services/data
- What data/service is provided by the API?
Instagram offers two types of APIs that allow an application to access data on the platform: the Instagram Graph API and the Instagram Basic Display API.
Previously, other APIs were available which allowed developers and researchers a less restricted access to data collection (Instagram (2021)). These older APIs are now depreciated.
The Instagram Basic Display API gives read-access to basic profile information, photos and videos on authenticated users’ accounts (Facebook for Developers (2021)). Particularly, it is possible to fetch a user’s profile, including fields like account type and account name, as well as a user’s media (images, videos and albums), including fields like media type, caption, URL and timestamp. The API does not allow to modify data like publishing media or moderating comments (see Instagram Graph API).
It is a RESTful API, meaning that queries are made for static information at the current moment. Queries are subject to rate limits. Responses are in the form of JSON-formatted objects containing the default and requested fields and edges.
14.2 Prerequisites
- What are the prerequisites to access the API (authentication)?
In order to access the Instagram Basic Display API, developers are required to first register as a Facebook developer on developers.facebook.com, to further create a Facebook App here and to submit the application for review.
Another prerequisite to access the API is to get authentication. API authentication is handled through Instagram User Access Tokens that conform to the OAuth 2.0 protocol. The process of getting an access token includes two parts. First, each application user must grant an application permission to read the user node and/or the media node. These permissions are controlled via an Authorization Window.
https://api.instagram.com/oauth/authorize
?client_id={appId}
&redirect_uri={redirectURI}
&scope=user_profile,user_media
&response_type=code
Give this URL to the application users. The next steps have to be completed by each user.
Open a new browser window and load the Authorization Window URL.
Authenticate your Instagram test user by signing into the Authorization Window
Click Authorize to grant your app access to your profile data.
Upon success, the page will redirect you to the redirect URI you included in the previous step and append an Authorization Code. For example: https://mmeierba.github.io/?code=AQD…#_
Copy the code except for the #_ portion in the end. Send this Authorization Code to researcher.
When a user successfully grants the application permission to access their data, the user is redirected to a redirect URI which appended with an Authorization Code. Second, the Authorization Code can be exchanged for a short-lived access token (i.e., valid for 1 hour).
Then, the API can be queried.
library(httr)
appId = "126..." #use Instagram App ID and secret (not Facebook App)
appSecret = "b73..."
redirectUri = "https://mmeierba.github.io/" #example
code = "AQD..."
id<-POST("https://api.instagram.com/oauth/access_token",
body=list(client_id=appId, client_secret=appSecret, grant_type="authorization_code", redirect_uri=redirectUri, code=code))
14.3 Simple API call
- What does a simple API call look like?
The Instagram Basic Display API is http-based. To query a node or edge, a GET()
call can be used. The base URLs are api.instagram.com and graph.instagram.com.
accessToken = "..."
userId = "..."
## Query the user node
GET("https://graph.instagram.com/userId?fields=id,username&access_token=accessToken")
GET("https://graph.instagram.com/me?fields=id,username&access_token=accessToken") #alternative
## Query the user media edge
GET("https://graph.instagram.com/me/media?fields=id,caption&access_token=accessToken")
## Query the user media node
mediaId = "..."
GET("https://graph.instagram.com/mediaId?fields=id,media_type,media_url,username,timestamp&access_token=accessToken")
14.4 API access in R
- How can we access the API from R (httr + other packages)?
The Instagram Graphic Display API can be accessed from R using the httr package.
Current R packages specific to APIs from Instagram (e.g., instaR) are related to deprecated versions of Instagram APIs and are therefore no longer useful in the current version (Instagram (2021)). To the knowledge of the authors, there are no R packages specific to the Instagram Basic Display API.
- Are there social science research examples using the API?
There are a couple of examples of studies in the field of social science that have used an Instagram API. However, the presented examples use older, depreciated versions of the Instagram API.
Hu, Manikonda, and Kambhampati (2014) collected 50 user profiles and their most recent photos, as well as users’ lists of friends and followers using the Instagram API. Analyzing the data, the authors identified a range of photo categories and types of Instagram users.
Ferwerda, Schedl, and Tkalcic (2015) used the Instagram API to extract Instagram pictures from survey participants, who had previously filled in a personality questionnaire. 113 survey participants granted the researchers access to their Instagram accounts through the API. The authors found that distinct features of the Instagram pictures (i.e., hue, brightness, saturation) associated with users’ personality traits.
These examples show the potential that extracting data using the Instagram Basic Display API has for social science researchers. Yet, a lot of data are not yet being leveraged (e.g., captions).