Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

pheuter commented Jun 27, 2010

where can i find client id and secret?

[Typo in technoweenie's URL] The OAuth Busy Developer's Guide to OAuth2 support is actually here. However, since the Github API is moving to version 3, you're probably better off consulting http://develop.github.com/.

cthiel commented Sep 22, 2011

I forked this gist to update it to oauth2 ~> 0.5.0 and also demo the github api v3: https://gist.github.com/4df21cf628cc3a8f1568

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 commented Feb 3, 2013

access this to request a token from facebook.

is

access this to request a token from github. ?

Wait forward ass

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