public
Last active

Sinatra Dropbox Oauth

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
require 'rubygems'
require 'erb'
require 'sinatra'
require 'dropbox'
 
configure do
enable :sessions
end
 
def authed?
redirect '/login' unless session[:dropbox_session]
dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
redirect '/login' unless dropbox_session.authorized?
end
 
get '/' do
erb :index
end
get '/admin' do
authed?
erb :admin
end
 
get '/login' do
if params[:oauth_token] then
dropbox_session = Dropbox::Session.deserialize(session[:dropbox_session])
dropbox_session.authorize(params)
session[:dropbox_session] = dropbox_session.serialize # re-serialize the authenticated session
 
redirect '/admin'
else
dropbox_session = Dropbox::Session.new('app_key', 'app_secret')
session[:dropbox_session] = dropbox_session.serialize
redirect dropbox_session.authorize_url(:oauth_callback => "#{@env['rack.url_scheme']}://#{request.host_with_port}/login")
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.