###Instagram APIができること
####①ユーザ
あなたのアプリケーションに登録したユーザが新しい写真を投稿するたびに通知されます。
####②タグ
写真に指定したタグがつけられるたびに通知されます。
####③場所
指定した場所にタグづけされた写真が投稿されるたびに通知されます。
####④地理情報
中心地点と半径で指定した任意の地理情報の写真が投稿されるたびに通知されます。
###APIの種類
####read 系
写真データ(写真、コメント、いいね)の閲覧
ユーザーデータ(フォローしている/されているユーザーのリスト)の閲覧
####comments 系
コメントの投稿
(自分の投稿した)コメントの削除
####relationship 系
指定ユーザーをフォローする
指定ユーザーのフォローをやめる
####likes 系
指定した写真を「いいね!」する
指定した写真の「いいね!」を取り消す
##アプリケーション登録をする
下記から登録
http://instagram.com/developer/
RegisterNewClientIDボタンを押しアプリケーション登録画面へ移動
Application Name
アプリ名。重複のない任意の名前を入力します。
Description
アプリの概要(説明文)
Website
アプリのサイトURL。適当でもOK
OAuth redirect_uri
コールバックURL。適当でもOK
登録が完了するとCLIENT ID, CLIENT SECRET が表示される
##アプリの作成手順
###①
まず、アプリを作成
$rails new instagram_test_app
###②
次にGemfileにinstagramのgemを追加
/Gemfile
gem 'instagram'
$bundle install
参考: https://github.com/Instagram/instagram-ruby-gem
###③
最初に取得したclient_idとclient_seacretを登録
/config/initializers 内にinstagram.rbを作成
/config/initializers/instagram.rb
require "instagram"
Instagram.configure do |config|
config.client_id = "INSTAGRAM_CLIENT_ID"
config.client_secret = "INSTAGRAM_CLIENT_SECRET"
end
(注: INSTAGRAM_CLIENT_IDとINSTAGRAM_CLIENT_SECRETは自分のを使用する)
###④
コントローラーを作成
$rails g controller home index
###⑤
ルーティングを更新
/config/routes
root 'home#index'
###⑥
コントローラーを編集
/app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@medias = Instagram.tag_recent_media(URI.encode("子ども"))
end
end
###⑦
viewを編集
/app/views/home/index.html.erb
<% @medias.each do |media| %>
<%= link_to media.link, :target => "_blank" do %>
<%= image_tag(media.images.low_resolution.url) %>
<% end %>
<% end %>
###⑧
これでタグ付けされた「子ども」の画像でいっぱいになったかと思います。
"子ども"と入れた部分を編集してみると楽しいですよ!
##番外編
##ユーザーIDから取得
自分のアカウントの画像をひっぱてくる
上記の⑤までは一緒
###⑥
コントローラーを編集
まずClient_idを下記から取得
http://jelled.com/instagram/lookup-user-id#
/app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@users = Instagram.user_recent_media("Client_id", {:count => 4})
end
end
{:count => 4}を入れることで枚数を制限できる
###⑦
viewを編集
/app/views/home/index.html.erb
<% @users.each do |user| %>
<%= image_tag(user.images.low_resolution.url) %>
<% end %>
###⑧
完成したはずです。
##場所のIDから取得
###⑥
location IDを取得するのに便利なサイト
http://maihamakyo.org/etc/locastagram/index.php
/app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@place = Instagram.location_recent_media(Location_id, {:count => 4})
end
end
Location_id は自分で取得する
###⑦
viewを編集
/app/views/home/index.html.erb
<% @place.each do |p| %>
<%= image_tag(p.images.low_resolution.url) %>
<% end %>
###⑧
完成
##ユーザー名からユーザー情報を取得
###⑥
/app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@user_name = Instagram.user_search("UserName")
end
end
Location_id は自分で取得する
###⑦
viewを編集
/app/views/home/index.html.erb
<%= @user_name.each do |name| %>
<%= image_tag('#{user.profile_picture}') %>
<% end %>
###⑧
完成
####周辺の位置情報からの取得もしたかったのですが、うまいやり方がわからなかった..。
###Thanks
https://s3-ap-northeast-1.amazonaws.com/nyansta-book/book.pdf
http://stackoverflow.com/questions/25812045/pulling-images-from-instagram-gem-over-secure-domain
http://jelled.com/instagram
http://watcher.moe-nifty.com/memo/2011/02/instagram-api.html
###Instagram日本語訳
http://www.dcrew.jp/ja-instagram-api-doc-v1/index.php/realtime