Exploring Instagram API using R!

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.

1.png

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

2


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 <- “”

code 1

Now run the  instaOAuth command in R. See below format.

code 3

For setting the ‘redirect_uri’. Go to Manage Clients.

Copy and paste http://localhost:1410/ into ‘redirect_uri’ on Instagram App Settings.

2.png

After setting the redirect uri press enter on R command window. Automatic browser will open below page & ask for the Authorization.

r 1

After Authorizing the app. Below message will come.

r 2

See below message “Authentication Complete”. Now the connection is done with the Instagram API & R.

comp.PNG

Extracting the Token from OAuth. Just type below command.

code 5

The token can be saved as a file in disk to be re-used in future sessions.

saving auth

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

json1.PNG

Output:

11111111111.png

Using Postman API Client

End Points: https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN

Output: JSON/XML

pstman.png

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:

json3.png

Actual Image:

sunburn.png


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.

output_wlPtEf.gif

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!

Advertisements

11 thoughts on “Exploring Instagram API using R!

  1. {“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?

    Like

      1. 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.

        Like

  2. Getting this error:

    {“code”: 400, “error_type”: “OAuthException”, “error_message”: “Invalid Client ID”}

    Any suggestions on how to get this fixed?

    Like

  3. 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

    Like

  4. 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?)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s