Rails Development with Facebook Canvas

Rails Development with Facebook Canvas



May 5, 2012


I was trying to make an existing app run as a Facebook App!

The following instructions/solutions will help you to access your http://localhost:3000/ through Facebook. In addition I would like to get the Facebook user with the fb_graph gem and save it in my database.

First of all you have to go to developers.facebook.com/apps to create a new Facebook test app.

The only important setting for now is this one:

Canvas Setting

I have also enabled the sandbox mode in the advanced settings, so that only I can access the Facebook app.

My app is really small so I’ve decided to only make an before_filter if the user uses Facebook to access my app:

class HomeController < ApplicationController
  before_filter :facebook_authorize


  def facebook_authorize
    return unless params[:signed_request]
    @auth = FbGraph::Auth.new FACEBOOK[:key], FACEBOOK[:secret]
    @auth = @auth.from_signed_request(params[:signed_request])
    if @auth.authorized?
      f = Facebook.find_or_initialize_by_identifier(@auth.user.identifier.try(:to_s))
      f.access_token = @auth.user.access_token.access_token
      session[:current_user] = f.id
      render :authorize

My Facebook Model only has two attributes: identifier and access_token.

If there is no params[:signed_request] nothing will happen because it is not a Facebook Canvas. Otherwise an unauthorized user (every user has to give explicit permission to use the app) have to get redirect to authorize the app:

The authorize.html.erb looks like this:

  top.location.href = '<%= @auth.authorize_uri("https://apps.facebook.com/YOUR_APP_NAME/").html_safe %>';

API Error Code: 191

API Error Description: The specified URL is not owned by the application Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.

I got this error when redirecting to the authorize url. The problem was my canvas url! I’ve used this:


This does not work with the App ID, you have to specify an App Namespace in the basic settings – then the error will disappear, like so (App Namespace is „app_localhost“):


Recent Blog Articles