Skip to content

Instantly share code, notes, and snippets.

@serradura
Created June 11, 2011 12:17
Show Gist options
  • Save serradura/1020511 to your computer and use it in GitHub Desktop.
Save serradura/1020511 to your computer and use it in GitHub Desktop.
Arquivos de Exemplo de como integrar aplicações Ruby ao Facebook
require 'rubygems'
require 'sinatra'
require 'haml'
require 'koala'
enable :sessions
set :facebook, {:app_id => 'Application ID',
:app_secret => 'Application Secret',
:callback_url => 'http://localhost:3000/canvas/'}
before do
authenticate!
end
post '/canvas/' do
haml :index
end
private
def authenticate!
if params.include?('signed_request') && oauth_access_token.include?('user_id')
session[:oauth_access_token] = oauth_access_token['oauth_token']
end
end
helpers do
def my_infos
graph.get_object('me')
end
def graph
Koala::Facebook::GraphAPI.new(session[:oauth_access_token])
end
def oauth
Koala::Facebook::OAuth.new(settings.facebook[:app_id], settings.facebook[:app_secret])
end
def oauth_access_token
oauth.parse_signed_request(params['signed_request'])
end
end
__END__
@@ layout
!!!
%html
%head
%title Test
%body
=yield
@@ index
%h1 Facebook - Website Application
%p
%h2
Seja bem vindo #{my_infos['name']}
%h2
Suas informações básicas são:
%ul
- my_infos.each do |key, value|
%li
<b>#{key}:</b> #{value}
%p
%a{:href=>"http://developers.facebook.com/docs/authentication/"} Documentação do Facebook sobre autenticação
%p
%h3
%a{:href=>"http://blog.serraduralabs.com"} Quer um tutorial completo?<br /> Acesse: http://blog.serraduralabs.com
require 'rubygems'
require 'sinatra'
require 'haml'
require 'koala'
enable :sessions
set :fb_app_id, 'Application ID'
set :fb_app_secret, 'Application Secret'
# A URL de callback não pode ser igual a URL de acesso configurada no Facebook
# caso contrário ocorrerá o ERRO:
# {
# "error": {
# "type": "OAuthException",
# "message": "Error validating verification code."
# }
# }
set :fb_callback_url, 'http://localhost:3000/authenticate'
get '/' do
haml :index
end
get '/authenticate' do
authenticate!
redirect to('/')
end
helpers do
def authenticate!
return if session[:oauth_access_token]
if params.include?('code')
session[:oauth_access_token] = oauth_access_token
end
end
def authenticated?
session[:oauth_access_token]
end
def my_infos
begin
graph.get_object('me')
rescue Koala::Facebook::APIError
session[:oauth_access_token] = nil
redirect to('/')
end
end
def graph
Koala::Facebook::GraphAPI.new(session[:oauth_access_token])
end
def oauth
Koala::Facebook::OAuth.new(settings.fb_app_id, settings.fb_app_secret, settings.fb_callback_url)
end
def oauth_access_token
oauth.get_access_token(params['code'])
end
end
__END__
@@ layout
!!!
%html
%head
%title Test
%body
=yield
@@ index
- if not authenticated?
:javascript
url = 'https://graph.facebook.com/oauth/authorize?';
client_id = 'client_id=#{settings.fb_app_id}';
redirect_uri = 'redirect_uri=#{settings.fb_callback_url}';
top.location.href = url + '&' + client_id + '&' + redirect_uri;
// -- Fim do Javascript
- if authenticated?
%h1 Facebook - Website Application
%p
%h2
Seja bem vindo #{my_infos['name']}
%h2
Suas informações básicas são:
%ul
- my_infos.each do |key, value|
%li
<b>#{key}:</b> #{value}
%p
%a{:href=>"http://developers.facebook.com/docs/authentication/"} Documentação do Facebook sobre autenticação
%p
%h3
%a{:href=>"http://blog.serraduralabs.com"} Quer um tutorial completo?<br /> Acesse: http://blog.serraduralabs.com
require 'rubygems'
require 'sinatra'
require 'haml'
require 'koala'
enable :sessions
set :facebook, {:app_id => 'Application ID',
:app_secret => 'Application Secret',
# A URL de callback não pode ser igual a URL de acesso configurada no Facebook
# caso contrário ocorrerá o ERRO:
# {
# "error": {
# "type": "OAuthException",
# "message": "Error validating verification code."
# }
# }
:callback_url => 'http://localhost:3000/authenticate'}
before '/' do
authorize!
end
post '/' do
haml :index
end
get '/' do
haml :index
end
get '/authorize' do
haml :authorize
end
get '/authenticate' do
authenticate!
redirect to('/')
end
private
def authorize!
if not authenticated?
renew_authorization
end
end
helpers do
def authenticate!
return if authenticated?
if params.include?('code')
session[:oauth_access_token] = oauth_access_token
end
end
def authenticated?
session[:oauth_access_token]
end
def my_infos
execute do
graph.get_object('me')
end
end
def execute
begin
yield
rescue Koala::Facebook::APIError
renew_authorization
end
end
def renew_authorization
session[:oauth_access_token] = nil
redirect to('/authorize')
end
def graph
Koala::Facebook::GraphAPI.new(session[:oauth_access_token])
end
def oauth
Koala::Facebook::OAuth.new(settings.facebook[:app_id], settings.facebook[:app_secret], settings.facebook[:callback_url])
end
def oauth_access_token
oauth.get_access_token(params['code'])
end
end
__END__
@@ layout
!!!
%html
%head
%title Test
%body
=yield
@@ authorize
:javascript
url = 'https://graph.facebook.com/oauth/authorize?';
client_id = 'client_id=#{settings.facebook[:app_id]}';
redirect_uri = 'redirect_uri=#{settings.facebook[:callback_url]}';
top.location.href = url + '&' + client_id + '&' + redirect_uri;
// -- Fim do Javascript
@@ index
%h1 Facebook - Website Application
%p
%h2
Seja bem vindo #{my_infos['name']}
%h2
Suas informações básicas são:
%ul
- my_infos.each do |key, value|
%li
<b>#{key}:</b> #{value}
%p
%a{:href=>"http://developers.facebook.com/docs/authentication/"} Documentação do Facebook sobre autenticação
%p
%h3
%a{:href=>"http://blog.serraduralabs.com"} Quer um tutorial completo?<br /> Acesse: http://blog.serraduralabs.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment