Created
September 29, 2010 18:15
-
-
Save erikeldridge/603228 to your computer and use it in GitHub Desktop.
ruby rack script that calls y! aouth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oauth -v 0.4.3 | |
json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define these in a .gems file for your Heroku app | |
require 'oauth' | |
require 'json' | |
class Router | |
def initialize(routes) | |
@routes = routes | |
end | |
def call(env) | |
@routes.each do |pattern, block| | |
match = env['REQUEST_PATH'].match(pattern) | |
if match | |
return block.call( env, match ) | |
end | |
end | |
return [ 404, {'Content-Type' => 'text/plain'}, 'error: file not found' ] | |
end | |
end | |
builder = Rack::Builder.new do | |
use Rack::CommonLogger | |
use Rack::ShowExceptions | |
use Rack::Lint | |
use Rack::Static, :urls => ["/static"] | |
run Router.new({ | |
%r{^/v1/request_auth$} => lambda do |env, match| | |
req = Rack::Request.new(env) | |
if defined?(req.params['oauth_consumer_key']).nil? | |
return 200, {'Content-Type' => 'application/json'}, '{"error":"oauth_consumer_key is a required param"}' | |
elsif defined?(req.params['oauth_consumer_secret']).nil? | |
return 200, {'Content-Type' => 'application/json'}, '{"error":"oauth_consumer_secret is a required param"}' | |
end | |
consumer = OAuth::Consumer.new( req.params['oauth_consumer_key'], req.params['oauth_consumer_secret'], { | |
:site => 'https://api.login.yahoo.com', | |
:request_token_path => '/oauth/v2/get_request_token', | |
:access_token_path => "/oauth/v2/get_token", | |
}) | |
# consumer.get_request_token throws exception for non-200 responses | |
begin | |
request_token = consumer.get_request_token({:oauth_callback => req.params['oauth_callback']}) | |
headers = { | |
'Content-Type' => 'text/plain', | |
'Location' => request_token.params['xoauth_request_auth_url'], | |
'Set-Cookie' => 'oauth_token_secret=%s;' % request_token.params['oauth_token_secret'] | |
} | |
[ 302, headers, '' ] | |
rescue Exception=>e | |
puts e.message | |
[ 200, {'Content-Type' => 'application/json'}, '{"error":"%s"}' % e.message ] | |
ensure | |
# as per http://www.caliban.org/ruby/rubyguide.shtml#exceptions | |
end | |
end, | |
%r{^/v1/get_token$} => lambda do |env, match| | |
req = Rack::Request.new(env) | |
if defined?(req.params['oauth_consumer_key']).nil? | |
return 200, {'Content-Type' => 'application/json'}, '{"error":"oauth_consumer_key is a required param"}' | |
elsif defined?(req.params['oauth_consumer_secret']).nil? | |
return 200, {'Content-Type' => 'application/json'}, '{"error":"oauth_consumer_secret is a required param"}' | |
end | |
consumer = OAuth::Consumer.new( req.params['oauth_consumer_key'], req.params['oauth_consumer_secret'], { | |
:site => 'https://api.login.yahoo.com', | |
:request_token_path => '/oauth/v2/get_request_token', | |
:access_token_path => "/oauth/v2/get_token", | |
}) | |
# consumer.get_request_token throws exception for non-200 responses | |
begin | |
request_token = OAuth::RequestToken.from_hash( consumer, { | |
:oauth_token => req.params['oauth_token'], | |
:oauth_token_secret => req.cookies['oauth_token_secret'] | |
}) | |
access_token = request_token.get_access_token({ :oauth_verifier => req.params['oauth_verifier'] }) | |
[ 200, {'Content-Type' => 'application/json'}, access_token.params.to_json ] | |
rescue Exception=>e | |
puts e.message | |
[ 200, {'Content-Type' => 'application/json'}, '{"error":"%s"}' % e.message ] | |
ensure | |
# as per http://www.caliban.org/ruby/rubyguide.shtml#exceptions | |
end | |
end, | |
# site root | |
%r{^/$} => lambda do |env, match| | |
request = Rack::Request.new( env ) | |
body = <<-END | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | |
"http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<body> | |
Aloha | |
</body> | |
</html> | |
END | |
[ 200, {'Content-Type' => 'text/html'}, body ] | |
end | |
}) | |
end | |
run builder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<!-- put this in a directory called "static" in your Heroku app --> | |
<html xmlns='http://www.w3.org/1999/xhtml'> | |
<head> | |
<link href='http://yui.yahooapis.com/2.8.1/build/reset-fonts-grids/reset-fonts-grids.css' rel='stylesheet' type='text/css' /> | |
<style type='text/css'> | |
/*<![CDATA[*/ | |
body { | |
font-size: 100%; | |
} | |
form { | |
margin: 1em; | |
padding: 1em; | |
-webkit-border-radius: 5px; | |
border: 1px solid #ccc; | |
} | |
div { | |
width: 100%; | |
overflow: hidden; | |
margin-bottom: 1ex; | |
} | |
label { | |
margin-top: 1ex; | |
float: left; | |
} | |
input.text { | |
float: right; | |
width: 64em; | |
padding: 1ex; | |
} | |
button { | |
float: left; | |
padding: 1ex; | |
} | |
.results { | |
float: left; | |
} | |
/*]]>*/ | |
</style> | |
</head> | |
<body> | |
<form action='/v1/request_auth'> | |
<div> | |
<label>OAuth consumer key:</label> | |
<input class='text' name='consumerKey' /> | |
</div> | |
<div> | |
<label>OAuth consumer secret:</label> | |
<input class='text' name='consumerSecret' /> | |
</div> | |
<div> | |
<label>OAuth callback url:</label> | |
<input class='text' name='callbackUri' /> | |
</div> | |
<div> | |
<button>Submit</button> | |
</div> | |
<pre></pre> | |
</form> | |
<form action='/v1/get_token'> | |
<div> | |
<label>OAuth consumer key:</label> | |
<input class='text' name='consumerKey' /> | |
</div> | |
<div> | |
<label>OAuth consumer secret:</label> | |
<input class='text' name='consumerSecret' /> | |
</div> | |
<div> | |
<label>OAuth token:</label> | |
<input class='text' name='token' /> | |
</div> | |
<div> | |
<label>OAuth verifier:</label> | |
<input class='text' name='verifier' /> | |
</div> | |
<div> | |
<button>Submit</button> | |
</div> | |
<pre></pre> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment