FacebookのOAuthを使うサンプルアプリ
これなら人の誕生日などが取れます。http://oauth.jp/facebookoauth-rails
でもサイトはすごい。右側でログインして使う。
githubから使うにはこのコマンドをしたところに「fb_graph_sample」フォルダが出来る。
以下のコマンドでダウンロードから実行までできます。
git clone https://github.com/nov/fb_graph_sample.git cd fb_graph_sample bundle install rails s
※以下の2ファイルは参考程度に見るだけでいじらない。
app/models/facebook.rb app/controllers/facebooks_controller.rb
config/facebook.yml
上記ファイルを編集する
client_idにはDeveloperサイトの「App ID」を記載
client_secretには「App Secret」を記載
permsはそのままでOK。
development: &defaults client_id: XXXXXXXXXXXXX client_secret: XXXXXXXXXXXXX perms: user_about_me,〜〜〜〜
・最後に以下のコマンドを実行
rake db:migrate
もし以下のエラーが出た場合
rake aborted! You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.
→Gemfileへ以下を記載する
gem 'rake', '0.9.0'
そして以下を実行
bundle update rake bundle install
→Rakefileも修正する
require File.expand_path('../config/application', __FILE__) require 'rake' module ::FbGraphSample class Application include Rake::DSL end end module ::RakeFileUtils extend Rake::FileUtilsExt end FbGraphSample::Application.load_tasks
最後にマイグレート※これしないとfacebookテーブルが作成されてないと怒られる
rake db:migrate rails s
終わり。
OAuthのキホン
理解してないなぁ。クリスチャンっぽい人の解説サイトhttp://gihyo.jp/dev/feature/01/oauth
・OAuthは,以下の特徴を持つ「認可情報の委譲」のための仕様
→信頼関係を構築したサービス間で
→ユーザの同意のもとにセキュアにユーザの権限を受け渡しする
3つの登場人物
1.OAuth Service Provider
→Facebookとか
2.OAuth Consumer
Providerから認可情報を受け取り,ユーザに代っていろいろな情報にアクセスしたり変更/追加を行う
→サイト側
3.User
→使う人
Service ProviderがConsumerに認可情報を渡許可や,すでに受け渡した認可情報を無効などができる
キホンのキ
OAuth1.0や2.0とサービスの対応付けの表がある。http://parashuto.com/rriver/social-media/learning-oauth-basics
index_urlでやるとRoutingエラーいみわかんね
<% form_tag index_url :method => 'get' do %>
config/routes.rb
Matcheese::Application.routes.draw do #こっち優先 post "users/index" get "users/oauth" get "users/callback" match 'users/:id', :to => "users#index", :as => "index" post "users/update" resources :users root :to => "users#oauth" end