Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created October 27, 2012 05:37
Show Gist options
  • Save rummelonp/3963081 to your computer and use it in GitHub Desktop.
Save rummelonp/3963081 to your computer and use it in GitHub Desktop.
ツイッターのアクセストークンとか割りと簡単に取得する感じのやつ
# -*- coding: utf-8 -*-
require 'sinatra/base'
require 'padrino-helpers'
require 'haml'
require 'oauth'
class Auth < Sinatra::Base
register Padrino::Helpers
set :haml, format: :html5
enable :sessions
enable :inline_templates
def consumer(data)
OAuth::Consumer.new(data[:key], data[:secret], {site: 'https://api.twitter.com'})
end
get '/' do
haml :index
end
post '/oauth' do
session[:consumer] = {
key: params[:key],
secret: params[:secret]
}
@consumer = consumer(session[:consumer])
@request_token = @consumer.get_request_token
session[:request] = {
token: @request_token.token,
secret: @request_token.secret
}
redirect @request_token.authorize_url
end
post '/verifier' do
@request_token = OAuth::RequestToken.new(consumer(session[:consumer]), session[:request][:token], session[:request][:secret])
@access_token = @request_token.get_access_token(oauth_verifier: params[:verifier])
session.delete :consumer
session.delete :request
haml :verifier
end
end
__END__
@@layout
!!!
%head
%body
= yield
@@index
- if session[:request] && session[:request]
- form_tag :verifier do
%p pin
%p= text_field_tag :verifier
%p= submit_tag
- else
- form_tag :oauth do
%p key
%p= text_field_tag :key
%p secret
%p= text_field_tag :secret
%p= submit_tag
@@verifier
%p= "access token: #{@access_token.token}"
%p= "access token secret: #{@access_token.secret}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment