I've been fighting with this for some hours now and I can't figure it out. I deployed a FB app via Heroku and everything worked fin but all of a sudden when you visit the site you get automatically redirected to FB login. Koala is throwing the following exception on page load:
require "sinatra"
require 'koala'
enable :sessions
set :raise_errors, false
set :show_exceptions, false
# Scope defines what permissions that we are asking the user to grant.
# In this example, we are asking for the ability to publish stories
# about using the app, access to what the user likes, and to be able
# to use their pictures. You should rewrite this scope with whatever
# permissions your app needs.
# for a full list of permissions
FACEBOOK_SCOPE = 'user_likes,user_photos,user_photo_video_tags,email,publish_actions'
unless ENV["FACEBOOK_APP_ID"] && ENV["FACEBOOK_SECRET"]
abort("missing env vars: please set FACEBOOK_APP_ID and FACEBOOK_SECRET with your app credentials")
end
before do
# HTTPS redirect
if settings.environment == :production && request.scheme != 'https'
puts "Redirect HTTPS"
redirect "https://#{request.env['HTTP_HOST']}"
end
end
helpers do
def host
request.env['HTTP_HOST']
end
def scheme
request.scheme
end
def url_no_scheme(path = '')
"//#{host}#{path}"
end
def url(path = '')
"#{scheme}://#{host}#{path}"
end
def authenticator
@authenticator ||= Koala::Facebook::OAuth.new(ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_SECRET"], url("/auth/facebook/callback"))
end
end
# the facebook session expired! reset ours and restart the process changing
error(Koala::Facebook::APIError) do
puts "<--------- ERROR ---------->"
session[:access_token] = nil
redirect "/auth/facebook"
end
get "/" do
# Get base API Connection
printf("<---------- Session %s ------------->",session[:access_token])
@graph = Koala::Facebook::API.new(session[:access_token])
puts "<--------- @ GRAPH ---------->"
# Get public details of current application
printf("<----------- ENV[FACEBOOK_APP_ID]: %s ------------->",ENV["FACEBOOK_APP_ID"])
@access_token = params[:access_token] || facebook_cookies[:access_token]
@app = @graph.get_object(ENV["FACEBOOK_APP_ID"])
printf("<----------- @app %s ------------>",@app)
puts "<--------- @ APP ---------->"
if session[:access_token]
@user = @graph.get_object("me")
@friends = @graph.get_connections('me', 'friends')
@photos = @graph.get_connections('me', 'photos')
@likes = @graph.get_connections('me', 'likes').first(4)
# for other data you can always run fql
@friends_using_app = @graph.fql_query("SELECT uid, name, is_app_user, pic_square FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1")
end
erb :index
end
get "/terms" do
erb :terms
end
get "/privacy" do
erb :privacy
end
# used by Canvas apps - redirect the POST to be a regular GET
post "/" do
puts "Redirect canvas"
redirect "/"
end
# used to close the browser window opened to post to wall/send to friends
get "/close" do
"<body onload='window.close();'/>"
end
get "/sign_out" do
session[:access_token] = nil
puts "Redirect signout"
redirect '/'
end
get "/auth/facebook" do
session[:access_token] = nil
puts "Redirect auth"
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
puts "callback Error"
redirect '/'
end
get '/plan_create' do
erb :plan_create
end
Hopefully someone smarter than me can help me figure this out. Thank you.