Skip to content

Instantly share code, notes, and snippets.

@technoweenie
Created April 27, 2010 21:48
Show Gist options
  • Save technoweenie/9fd1a6199da0465ec87c to your computer and use it in GitHub Desktop.
Save technoweenie/9fd1a6199da0465ec87c to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'oauth2'
require 'json'
def new_client
OAuth2::Client.new('CLIENT ID', 'CLIENT SECRET', :site => 'https://github.com',
:authorize_path => '/login/oauth/authorize', :access_token_path => '/login/oauth/access_token')
end
get "/" do
%(<p>Update the <code>#new_client</code> method in the sinatra app and <a href="/auth/github">try to authorize</a>.</p>)
end
# access this to request a token from facebook.
get '/auth/github' do
url = new_client.web_server.authorize_url(
:redirect_uri => redirect_uri,
:scope => 'email,offline_access'
)
puts "Redirecting to URL: #{url.inspect}"
redirect url
end
# If the user authorizes it, this request gets your access token
# and makes a successful api call.
get '/auth/github/callback' do
begin
access_token = new_client.web_server.get_access_token(params[:code], :redirect_uri => redirect_uri)
user = JSON.parse(access_token.get('/api/v2/json/user/show'))
"<p>Your OAuth access token: #{access_token.token}</p><p>Your extended profile data:\n#{user.inspect}</p>"
rescue OAuth2::HTTPError
%(<p>Outdated ?code=#{params[:code]}:</p><p>#{$!}</p><p><a href="/auth/github">Retry</a></p>)
end
end
def redirect_uri(path = '/auth/github/callback', query = nil)
uri = URI.parse(request.url)
uri.path = path
uri.query = query
uri.to_s
end
@zhangyuxiu
Copy link

I tried this example in ruby. But I got a problem in line 17:
NoMethodError - undefined method `web_server' for #OAuth2::Client:0xb7420c18:

What should I do to solve the problem?

@shinriyo
Copy link

shinriyo commented Feb 3, 2013

access this to request a token from facebook.

is

access this to request a token from github. ?

@tamernasr
Copy link

Wait forward ass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment