Folks,
In this blog we will explore the basics of Instagram API using R.
“instaR” Package in R: Provides an interface to the Instagram API , which allows R users to access public users’ profile data.
Install “instaR” package from CRAN : install.packages(“instaR”)
Install “RCurl” package from CRAN : install.packages(“RCurl”)
Step 1: Registering an Application with Instagram.
If you already have an account with Instagram, go to Instagram Developer and register.
Click “Register your application” button. After you register as a Instagram developer, you can go to a Manage clients & register a new client.
Note down your App Name, Client ID & Client Secret
Step 2: Create OAuth token to Instagram R session.
instaOAuth creates an OAuth access token that enables R to make authenticated calls to the Instagram API.
instaOAuth( client_id, client_secret, scope = “basic”)
Scope is related to the access permissions. Read Login Permissions (Scopes) Link
my_app_client_id <- “”
my_app_client_secret <- “”
Now run the instaOAuth command in R. See below format.
For setting the ‘redirect_uri’. Go to Manage Clients.
Copy and paste http://localhost:1410/ into ‘redirect_uri’ on Instagram App Settings.
After setting the redirect uri press enter on R command window. Automatic browser will open below page & ask for the Authorization.
After Authorizing the app. Below message will come.
See below message “Authentication Complete”. Now the connection is done with the Instagram API & R.
Extracting the Token from OAuth. Just type below command.
The token can be saved as a file in disk to be re-used in future sessions.
Now we have token = my_access_token. So here we goes with some of the basic end points of Instagram API.
Instagram End Points Links: Here
1) Getting the information from Instagram for token owner.
End Points: https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN
Using R
Output:
Using Postman API Client
End Points: https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN
Output: JSON/XML
2) Get the most recent media published on Instagram by token owner.
https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN
Using Postman API Client
Output:
Actual Image:
Checking the details of any public image.
Hit this endpoint: https://api.instagram.com/oembed/?url=YOUR URL
See below gif for explanation. I’m using postman client for API testing.
You can get/post API request in your own way! Explore more End Points here: Link
PS: In Sandbox mode we cannot use extended permissions. Read Login Permissions (Scopes) here Link
Thanks!
Happy Learning!
Follow @shobhitsinghIN
got error when try to access this URL :
https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN
The error was : {“code”: 403, “error_type”: “OAuthForbiddenException”, “error_message”: “Invalid signed-request: Missing required parameter ‘sig'”}
LikeLiked by 1 person
Please check your access token.
LikeLike
I have handle this. I put option on RCurl library and it solve this problem.
LikeLike
{“meta”:{“error_type”:”OAuthParameterException”,”code”:400,”error_message”:”The client_id provided is invalid and does not match a valid application.”}}
client_id is invalid?
LikeLike
How you are Extracting the Token from OAuth. Please check your access token.
LikeLike
Using this ig_oauth <- instaOAuth(ig_client_id, ig_client_secret, scope = "basic")
It can't go wrong. But, I get 0 post when extracting anything.
LikeLike
Getting this error:
{“code”: 400, “error_type”: “OAuthException”, “error_message”: “Invalid Client ID”}
Any suggestions on how to get this fixed?
LikeLike
I have no idea. I get this problem too. Anyone please help me
LikeLike
I need data from a particular brand page. So, I used URL in paste():
https://api.instagram.com/v1/users/search?q=jack&access_token=“, app_access_token.
I am getting empty list irrespective of existing usernames on Instagram. Below is the code after autentication process complete:
userinfo data json json
$meta
$meta$code
[1] 200
$data
list()
LikeLike
Hi Man! I am excited with your tutorial!
I will try this weekend and see if it is working for me.
Can I use the RStudio?
Regards,
Ricardo
LikeLike
I am getting this error while API and R connection
> Auth<- instaOAuth(client_id,client_secret,scope ="basic")
Copy and paste into 'OAuth redirect_uri' on Instagram App Settings: http://localhost:1410/
When done, press any key to continue…
Waiting for authentication in browser…
Press Esc/Ctrl + C to abort
Authentication complete.
Error in curl::curl_fetch_memory(url, handle = handle) :
Problem with the SSL CA cert (path? access rights?)
LikeLike
how to retrieve other people’s account data instead of own account using R?
LikeLike
Muchas gracias. ?Como puedo iniciar sesion?
LikeLike