LoginSignup
90
104

More than 5 years have passed since last update.

InstagramのAPIを使って画像をひっぱてくる

Last updated at Posted at 2015-02-04

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日本語訳

90
104
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
90
104