Created
December 16, 2011 16:39
-
-
Save andrewytliu/1486777 to your computer and use it in GitHub Desktop.
sinatra with rest-graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
require "rest-graph" | |
enable :sessions | |
# set :raise_errors, false | |
# set :show_exceptions, false | |
FACEBOOK_SCOPE = 'read_stream' | |
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 | |
@rg = RestGraph.new(:access_token => session[:access_token], | |
:app_id => ENV["FACEBOOK_APP_ID"], | |
:secret => ENV["FACEBOOK_SECRET"]) | |
unless request.scheme == 'https' | |
redirect "https://#{request.env['HTTP_HOST']}" | |
end | |
end | |
helpers do | |
def full_url path | |
base = "#{request.scheme}://#{request.env['HTTP_HOST']}" | |
base + path | |
end | |
end | |
# the facebook session expired! reset ours and restart the process | |
error(RestGraph::Error::AccessToken) do | |
redirect "/login" | |
end | |
get "/" do | |
# if not logged in | |
redirect "/login" unless @rg.authorized? | |
@rg.get("me") | |
end | |
post "/" do | |
redirect "/" | |
end | |
# facebook login | |
get "/login" do | |
session[:access_token] = nil | |
redirect @rg.authorize_url(:scope => FACEBOOK_SCOPE, | |
:redirect_uri => full_url("/callback")) | |
end | |
# facebook callback | |
get "/callback" do | |
@rg.authorize!(:redirect_uri => full_url("/callback"), | |
:code => params[:code]) | |
session[:access_token] = @rg.access_token | |
redirect '/' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment