canvas app work, need to write tests for it
This commit is contained in:
parent
0937d2ba9d
commit
6a3f2f73d5
37
README
37
README
@ -48,4 +48,41 @@ shared login partial.
|
||||
<%= fb_login_and_redirect('<your URL here>', :perms => 'email,user_birthday') %>
|
||||
<% end %>
|
||||
|
||||
Using with Canvas Applications
|
||||
==============================
|
||||
|
||||
To improve integration with Facebook and iframe canvas applications, the primary goal
|
||||
being things like FB.ui work as a dialog rather than a popup, the application
|
||||
needs to be authenticated against the user's account via OAuth before use.
|
||||
|
||||
0. Prerequisite: You need a Facebook app. Have your canvas page name handy.
|
||||
|
||||
1. Install facebooker2.
|
||||
|
||||
2. Create config/facebooker.yml as above, but add the following key:
|
||||
|
||||
production:
|
||||
canvas_page_name: <your canvas page name>
|
||||
|
||||
3. Add the following lines to your app/controllers/application_controller.rb
|
||||
|
||||
include Facebooker2::Rails::Controller::CanvasOAuth
|
||||
|
||||
ensure_canvas_connected_to_facebook :oauth_url, 'publish_stream'
|
||||
create_facebook_oauth_callback :oauth
|
||||
|
||||
rescue_from Facebooker2::OAuthException do |exception|
|
||||
redirect_to 'http://www.facebook.com/'
|
||||
end
|
||||
|
||||
4. Create a route that generates a URL for the OAuth callback and calls the appropriate
|
||||
action on your controller:
|
||||
|
||||
map.oauth '/oauth', :controller => :application, :action => :oauth
|
||||
|
||||
5. Your canvas application will now ensure that the current user has authorized
|
||||
the application before anything else is allowed. The authorization and FB.ui dialogs
|
||||
will appear inline instead of as popups, improving user experience.
|
||||
|
||||
Copyright (c) 2010 Mike Mangino, released under the MIT license
|
||||
Copyright (c) 2010 John Bintz, released under the MIT license
|
||||
|
@ -1,9 +1,10 @@
|
||||
# Facebooker2
|
||||
require "mogli"
|
||||
|
||||
module Facebooker2
|
||||
class NotConfigured < Exception; end
|
||||
class << self
|
||||
attr_accessor :api_key, :secret, :app_id
|
||||
attr_accessor :api_key, :secret, :app_id, :canvas_page_name
|
||||
end
|
||||
|
||||
def self.secret
|
||||
@ -22,6 +23,7 @@ module Facebooker2
|
||||
self.api_key = hash[:api_key]
|
||||
self.secret = hash[:secret]
|
||||
self.app_id = hash[:app_id]
|
||||
self.canvas_page_name = hash[:canvas_page_name]
|
||||
end
|
||||
|
||||
def self.load_facebooker_yaml
|
||||
@ -43,8 +45,10 @@ end
|
||||
|
||||
|
||||
require "facebooker2/rails/controller"
|
||||
require "facebooker2/rails/controller/canvas_oauth"
|
||||
require "facebooker2/rails/helpers/facebook_connect"
|
||||
require "facebooker2/rails/helpers/javascript"
|
||||
require "facebooker2/rails/helpers/request_forms"
|
||||
require "facebooker2/rails/helpers/user"
|
||||
require "facebooker2/rails/helpers"
|
||||
require "facebooker2/oauth_exception"
|
||||
|
4
lib/facebooker2/oauth_exception.rb
Normal file
4
lib/facebooker2/oauth_exception.rb
Normal file
@ -0,0 +1,4 @@
|
||||
module Facebooker2
|
||||
class OAuthException < StandardError; end
|
||||
end
|
||||
|
@ -107,6 +107,7 @@ module Facebooker2
|
||||
fb_create_user_and_client(facebook_params[:oauth_token],facebook_params[:expires],facebook_params[:user_id])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
55
lib/facebooker2/rails/controller/canvas_oauth.rb
Normal file
55
lib/facebooker2/rails/controller/canvas_oauth.rb
Normal file
@ -0,0 +1,55 @@
|
||||
module Facebooker2
|
||||
module Rails
|
||||
module Controller
|
||||
module CanvasOAuth
|
||||
def self.included(controller)
|
||||
controller.extend(CanvasOAuthClass)
|
||||
|
||||
class << controller
|
||||
attr_accessor :_facebooker_oauth_callback_url, :_facebooker_scope
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def canvas_oauth_connect
|
||||
raise "Canvas page name not defined! Define it in config/facebooker.yml as #{::Rails.env}: canvas_page_name: <your url>." if !Facebooker2.canvas_page_name
|
||||
if params[:error]
|
||||
raise Facebooker2::OAuthException.new(params[:error][:message])
|
||||
else
|
||||
redirect_to ('http://apps.facebook.com/' + Facebooker2.canvas_page_name) if params[:code]
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
def ensure_canvas_connected
|
||||
case self.class._facebooker_oauth_callback_url
|
||||
when Symbol
|
||||
callback_url = send(self.class._facebooker_oauth_callback_url)
|
||||
end
|
||||
|
||||
if current_facebook_user == nil && !params[:code] && !params[:error]
|
||||
render :text => "<script>top.location.href = 'https://graph.facebook.com/oauth/authorize?client_id=#{Facebooker2.app_id}&redirect_uri=#{callback_url}&scope=#{[ self.class._facebooker_scope ].flatten * ','}'</script>"
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module CanvasOAuthClass
|
||||
def ensure_canvas_connected_to_facebook(oauth_callback_url, *scope)
|
||||
self._facebooker_oauth_callback_url = oauth_callback_url
|
||||
self._facebooker_scope = scope
|
||||
|
||||
before_filter :ensure_canvas_connected
|
||||
end
|
||||
|
||||
def create_facebook_oauth_callback(method_name)
|
||||
self.class_eval(<<-EOT)
|
||||
def #{method_name}
|
||||
return canvas_oauth_connect
|
||||
end
|
||||
EOT
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
0
spec/rails/controller/canvas_oauth_spec.rb
Normal file
0
spec/rails/controller/canvas_oauth_spec.rb
Normal file
Loading…
Reference in New Issue
Block a user