LoginSignup
24
26

More than 5 years have passed since last update.

Koalaを使ってFacebookAPIにアクセスする.

Posted at

最近FacebookのAPIに触れる事が多いので,それに関して簡単にまとめました.
というか,FacebookAPIとGraphAPIて一緒何ですかね...何か違う気がしますが

環境と必要なもの

自分の情報を出力するサンプルプログラム

trykoala.rb
require 'koala'

access_token = 'GraphAPIのアクセストークン'
graph = Koala::Facebook::API.new(access_token)
profile = graph.get_object('me')

puts profile['id']
puts profile['name']

Koala::Facebook::API.new(access_token)でAPIオブジェクトを作ってgraphに入れています.
graph.get_object('me')で自分に関する情報を取得して,profileに入れています.
あとは'id'とか'name'でFacebookAPIのUIDと自分の名前を出力しています.
ちなみに,これで取ってくる自分の情報は多分こんだけあるはずです...!

get_object('me')で返ってくるハッシュの中身

キー 内容
id UID
bio ユーザーの経歴
birthday 誕生日
education ユーザーの学歴リスト
email メールアドレス(emailパーミッションが必要??)
name 名前
first_name ファーストネーム
last_name ラストネーム
gender 性別
hometown 出身地
interested_in 興味のある性別(恋愛対象)
languages 母国語
link ユーザーのFacebookプロフィールのURL
location 現住所
locale ユーザーのlocale
relationship_status 交際ステータス
quotes お気に入りの言葉
timezone タイムゾーン
update_item プロフィールの最終更新日時
username ユーザ名
verified アカウントのverificationステータス

自分のプロフィール写真のURLを抽出するサンプルプログラム

trykoala.rb
require 'koala'

access_token = 'GraphAPIのアクセストークン'
graph = Koala::Facebook::API.new(access_token)
picture_url = graph.get_picture('me')

puts picture_url

graph.get_picture('me')
で自分のプロフィール写真を取ってくる事が出来ます.
ちなみに,
graph.get_picture('me', :type => 'square')
と同じ意味です.
つまり! square以外にも写真サイズを指定する事が出来るのです!!

指定出来るvalue
small
normal
large
square (指定しなければこれがデフォルト)

同じFacebookAppを使っている友達を出力するサンプルプログラム

注意!!
これは純粋に自分の友達全員を取ってこれるわけではありません!!
同じFacebookアプリケーションを使っている友達しかとってこれません.
こちらに詳細を書きました.

trykoala.rb
require 'koala'

access_token = 'GraphAPIのアクセストークン'
graph = Koala::Facebook::API.new(access_token)
friends = graph.get_connections('me', 'friends', :local => 'ja-jp')
friends.each do |friend|
    puts friend['name']
end

これで同じFacebookアプリケーションを使っている友達を日本語で出力する事が出来ます!
案外簡単ですね!

注意

FacebookのAPIは頻繁に仕様変更があるようです.
注意してください!!

24
26
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
24
26