Install facebooker2 for Rails 3

0. Prerequisite: You need a facebook app. Have your API Key, Application Secret, and Application ID handy.
1. put in your Gemfile

gem 'facebooker2'
gem 'mogli'

2. next open your terminal and run bundle install
3. Create config/facebooker.yml with the appropriate environment.

  app_id: <your application id>
  secret: <your application secret>
  api_key: <your application key>
  canvas_page_name: <canvas page>
  callback_domain: < url call_back >
  pretty_errors: false
  set_asset_host_to_callback_url: true

4. Create config/initializers/facebooker2.rb and place the following line in it


5. Add the following line to your app/controllers/application_controller.rb add it right after the line class

class ApplicationController < ActionController::Base
  include Facebooker2::Rails::Controller
  before_filter :set_p3p_header_for_third_party_cookies  
  before_filter :ensure_authenticated_to_facebook 

  #each time a user visits, we will refresh their access token
  #1 - check for a user_id from the signed_request
  #2 - check the session for an active user
  #3 - nothing worked. redirect to the auth page.
  def ensure_authenticated_to_facebook
    if current_facebook_user == nil "no auth token, session, or cookie found."
      top_redirect_to auth_url

  #creates the oauth url for the user to request authorize and authenticate
  # more details on the scope and display options can be found here:
  def auth_url
    url = authenticator.authorize_url(:scope => 'offline_access,email,publish_stream', :display => 'page') "redirecting to " + url
    return url

  def authenticator
    # by redirecting back to HTTP_REFERER, we will go back to the the request!
    # if there is no referrer, send this request url as the callback url
    request_path = @_request.env["REQUEST_PATH"].blank? ? "/" : @_request.env["REQUEST_PATH"]
    redirect_url = (@_request.env["HTTP_REFERER"] != nil ? @_request.env["HTTP_REFERER"] :  @_request.env["rack.url_scheme"] + "://" + @_request.env["HTTP_HOST"] + request_path)
    @authenticator ||=, Facebooker2.secret, redirect_url)    

  # Redirects the top window to the given url if the content is in an iframe, otherwise performs
  # a normal redirect_to call.
  def top_redirect_to(url)
      render :layout => false, :inline => '<html><head><script type="text/javascript"> = '+

Facebook canvas applications

If you are building an application that runs inside a Facebook canvas, all the coming requests from Facebook to your iframe will be POST requests.

You can use the PostCanvas rack middleware to turn the Facebook POST requests back to GET requests and keep your app restful as described in this blog post. If you are using Rails 3, put this line of code inside your file :

use Rack::PostCanvas

Also, if you plan on supporting IE 6/7 and use cookie authentication, you should add a P3P header to your response in order for IE to accept the cookie :

before_filter :set_p3p_header_for_third_party_cookies

About me
Interested about Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: