require 'bundler/setup' require 'sinatra/base' require 'omniauth-facebook' SCOPE = 'email,read_stream' class App < Sinatra::Base # turn off sinatra default X-Frame-Options for FB canvas set :protection, :except => :frame_options # server-side flow get '/' do # NOTE: you would just hit this endpoint directly from the browser # in a real app. the redirect is just here to setup the root # path in this example sinatra app. redirect '/auth/facebook' end # client-side flow get '/client-side' do content_type 'text/html' # NOTE: when you enable cookie below in the FB.init call # the GET request in the FB.login callback will send # a signed request in a cookie back the OmniAuth callback # which will parse out the authorization code and obtain # the access_token. This will be the exact same access_token # returned to the client in response.authResponse.accessToken. <<-END Client-side Flow Example

Connect to FB

END end # auth via FB canvas and signed request param post '/canvas/' do # we just redirect to /auth/facebook here which will parse the # signed_request FB sends us, asking for auth if the user has # not already granted access, or simply moving straight to the # callback where they have already granted access. redirect "/auth/facebook?signed_request=#{request.params['signed_request']}" end get '/auth/:provider/callback' do content_type 'application/json' MultiJson.encode(request.env) end get '/auth/failure' do content_type 'application/json' MultiJson.encode(request.env) end end use Rack::Session::Cookie use OmniAuth::Builder do provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => SCOPE end run App.new