Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
7kai Tasks OAuth sample for sinatra
# -rubygems を指定しなくても起動する様に
require 'rubygems' unless defined? ::Gem
require 'sinatra' # gem install sinatra
# require "sinatra/reloader" if development? # gem install sinatra-contrib
require 'erubis'
# require 'mysql2-cs-bind' # gem install mysql2-cs-bind
require 'rack/csrf' # gem install rack_csrf (CSRF対策)
require 'oauth' # gem install oauth
require 'json'
set :erb, :escape_html => true
configure do
use Rack::Session::Cookie, :secret => Digest::SHA1.hexdigest(rand.to_s)
use Rack::Csrf, :raise => true
end
callback_url = 'http://127.0.0.1:4567/callback'
consumer = OAuth::Consumer.new(
'YOUR_APP_CONSUMER_KEY',
'YOUR_APP_CONSUMER_SECRET',
:site => 'https://tasks.7kai.org')
get '/' do
request_token = consumer.get_request_token(:oauth_callback => callback_url)
session[:request_token] = request_token
redirect request_token.authorize_url
end
get '/callback' do
request_token = session[:request_token]
access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
response = access_token.get('/api/1/account/me')
response.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment