This article introduce about API of Instagram. After reading this artile, you will know:
Overview about Instagram API.
How to interacte with Instagram API.
How to use this API in Rails application.
Instagram is a sharing-application, social networking service which enables users to take pictures and videos, and share them on a variety of social networks.
It was acquired by Facebook in April 2012 for approximately $1 billion in cash and stock. 
Instagram API support to we a interface to interact with data from Instagram.
To start using the API, we have 3 basic step:
Go to the Manage Clients to register a new Client: Manage Clients
After registering, we have information about: Client ID and Client Secret which will be used to authenticate in next step.
Authenticated requests require an access_token. These tokens are unique to a user and should be stored securely. Access tokens may expire at any time in the future.
In order to receive access_token, we have to do 2 steps:
Step 1: Direct your user to our authorization URL
Access to URL with format:
Step 2: Receive the access_token via the URL fragment
After we have authenticated and then authorized your application, Instagram redirects them to your redirect_uri with the access_token in the url fragment. It will look like this:
When we authorized application, now we can be able to interact with Instagram API Endpoints.
Endpoints allows we interact with data about: Users, Media, Comments, Likes, Tags, Locations and more.
In next section, I’ll demo a Rails application using Media Endpoints of Instagram API.
In this section, I'll guide you implement a Rails application to get recent media (photos) from Instagram have hashtag is “#fashion”.
Step 1: Add gem 'instagram' (instagram-ruby-gem)
This is gem which support a Ruby wrapper for the Instagram REST and Search APIs.
You can get more information in rubydoc-instagram-ruby-gem.
Step 2: Add instagram.rb config file in config/initializers.
Instagram.configure do |config| config.client_id = CLIENT_ID config.client_secret = CLIENT_SECRET config.access_token = ACCESS_TOKEN end
Step 3: Write a method to get recent media which tag is 'fashion'
We'll create Photo model with 3 fields are thumbnail_url, owner and link to save data which get from Instagram API.
rails generate model Photo thumbnail_url:string owner:string link:string
To get list of recent media items which have 'fashion' tag, we use tag_recent_media method of instagram-ruby-gem. Details function in below:
class Photo < ActiveRecord::Base validates :thumbnail_url, :owner, :link, :presence => true def get_recent_media recent_media = Instagram.tag_recent_media('fashion') recent_media.each do |media| Photo.create( link: media.link, owner: media.caption.from.username, thumbnail_url: media.images.thumbnail.url ) end end end
I hope so this article helps you quickly learn and interact with Instagram API :)