Skip to content

Instantly share code, notes, and snippets.

@nov
Created March 6, 2011 13:17
Show Gist options
  • Save nov/857277 to your computer and use it in GitHub Desktop.
Save nov/857277 to your computer and use it in GitHub Desktop.
OAuth2 Client for Rack::OAuth2 Sample Server
require 'rest-client'
base_url = 'http://rack-oauth2-sample.heroku.com'
redirect_uri = 'http://client.example.com/callback'
client_id = SET_YOUR_OWN
client_secret = SET_YOUR_OWN
code = SET_YOUR_OWN
token = SET_YOUR_OWN
def url_for(path)
File.join(base_url, path)
end
begin
res = RestClient.post url_for('/oauth2/token'), {
:grant_type => :authorization_code,
:client_id => client_id,
:client_secret => client_secret,
:code => code,
:redirect_uri => redirect_uri
}
p res.body
rescue => e
p e.response
end
begin
res = RestClient.post url_for('/protected_resources'), {:oauth_token => token, :data => 'data'}
p res, res.headers
rescue => e
p e.response
end
begin
res = RestClient.get url_for('/protected_resources'), 'Authorization' => "Bearer #{token}"
p res
rescue => e
p e.response
end
begin
res = RestClient.get url_for('/protected_resources/1'), 'Authorization' => "Bearer #{token}"
p res
rescue => e
p e.response
end
begin
res = RestClient.delete url_for('/protected_resources/1'), 'Authorization' => "Bearer #{token}"
p res
rescue => e
p e.response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment