public
Last active — forked from technoweenie/oauth2_example.rb

  • Download Gist
oauth2_example.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# see http://github.com/intridea/oauth2
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
 
class ConnectionLogger < Faraday::Middleware
def call(env)
env[:response].on_complete do |env|
puts "RESULT: #{env[:status]}\n#{env[:body]}"
end
 
process_body_for_request(env)
puts "#{env[:method].inspect} #{env[:url].to_s}"
puts env[:request_headers].inspect if !env[:request_headers].empty?
puts env[:body] if env[:body]
@app.call env
end
end
 
$client = OAuth2::Client.new('120094574673767', 'b54dc82476af2814e620b86776c42c0e', :site => 'https://graph.facebook.com', :adapter => :test)
$client.connection.build do |b|
b.use ConnectionLogger
b.adapter :net_http
end
 
get '/auth/facebook' do
url = $client.web_server.authorize_url(
:redirect_uri => redirect_uri,
:scope => 'email,offline_access'
)
puts "Redirecting to URL: #{url.inspect}"
redirect url
end
 
get '/auth/facebook/callback' do
access_token = $client.web_server.access_token(params[:code], :redirect_uri => redirect_uri)
user = JSON.parse(access_token.get('/me'))
 
user.inspect
end
 
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/facebook/callback'
uri.query = nil
uri.to_s
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.