Skip to content

Instantly share code, notes, and snippets.

@komagata
Created September 22, 2011 08:32
Show Gist options
  • Save komagata/1234340 to your computer and use it in GitHub Desktop.
Save komagata/1234340 to your computer and use it in GitHub Desktop.
oauth2 sample
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
configure :production do
@@client = OAuth2::Client.new(
'xxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxx',
:site => 'https://graph.facebook.com'
)
end
configure :development, :test do
@@client = OAuth2::Client.new(
'xxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxx',
:site => 'https://graph.facebook.com'
)
end
def redirect_uri
uri = URI.parse(request.url)
uri.path = '/auth/facebook/callback'
uri.query = nil
uri.to_s
end
get '/' do
'<a href="/auth/facebook">login</a>'
end
post '/' do
'<a href="/auth/facebook">login</a>'
end
get '/auth/facebook' do
redirect @@client.web_server.authorize_url(
:redirect_uri => redirect_uri,
:scope => 'email,offline_access'
)
end
get '/auth/facebook/callback' do
access_token = @@client.web_server.get_access_token(
params[:code],
:redirect_uri => redirect_uri
)
user = JSON.parse(access_token.get('/me'))
user.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment