Skip to content

Instantly share code, notes, and snippets.

@phobeo
Created April 18, 2011 13:57
Show Gist options
  • Save phobeo/925393 to your computer and use it in GitHub Desktop.
Save phobeo/925393 to your computer and use it in GitHub Desktop.
ruby file (in server).rb
require 'rubygems'
require 'sinatra'
require 'oauth'
require 'json'
require 'haml'
require 'ruby-debug'
enable :sessions
TWITTER_CONSUMER_KEY = "asac8z2Tw9XR2LflFmNVsw"
TWITTER_CONSUMER_SECRET = "UXrogLs4teSmbKo30ZwFYekzorOPsCNUPLBPz0Q"
before do
@consumer = OAuth::Consumer.new(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET,
:site => "https://api.twitter.com",
:scheme => :header,
:http_method => :post,
:request_token_path => "/oauth/request_token",
:access_token_path => "/oauth/access_token",
:authorize_path => "/oauth/authorize")
end
get '/' do
haml :index
end
get '/index.html' do
"returned params - token:#{params[:oauth_token]} - secret:#{params[:oauth_secret]}"
end
get '/login' do
request_token = @consumer.get_request_token :oauth_callback => auth_url
session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret
redirect request_token.authorize_url
end
get '/auth' do
request_token = OAuth::RequestToken.new(
@consumer, session[:request_token], session[:request_token_secret]
)
access_token = @consumer.get_access_token(
request_token, :oauth_verifier => params[:oauth_verifier]
)
puts "--- token is #{access_token.to_query}"
redirect "/index.html?#{access_token.to_query}"
#session[:profile] = JSON.parse(
#access_token.get("/account/verify_credentials.json").body
#)
end
def auth_url
uri = URI.parse(request.url)
uri.path = '/auth'
uri.query = nil
uri.to_s
end
__END__
@@ layout
%html
= yield
@@ index
%form{:action => "/login", :method => "get"}
%input{:type => "submit", :value => "Connect with Twitter"}
@@ profile
%img{ :src => "#{profile["profile_image_url"]}", :align => "left"}
%p Welcome #{profile["name"]}!
%p You are now connected via Twitter
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function startAuth() {
try {
window.location = "http://174.143.206.161:4567/login"
} catch(e) {
window.alert(e)
}
}
function checkParams() {
var qKeys = {}
var re = /[?&]([^=]+)(?:=([^&]*))?/g
var matchInfo
while(matchInfo = re.exec(window.location.search)){
qKeys[matchInfo[1]] = matchInfo[2]
}
if(qKeys.length == 0) {
window.alert("no params found")
} else {
for(var key in qKeys) {
window.alert("received key: "+key+" (val: "+qKeys[key]+")")
}
}
}
</script>
<style type="text/css">
body {
background:lightgray;
}
</style>
</head>
<body onload="checkParams()">
<h2>oauth test</h2>
<div id="content">
press <a id="auth" href="#" onclick="startAuth()">here to start</a>
<br/>
alternative: <a href="http://174.143.206.161:4567/login">normal link</a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment