Dropbox uploading files
Dropbox uploading files=>
1. create developer account on dropbox (create credentials).
2. put credentials in app.rb
DROPBOX_APP_KEY = "1ikbatyv1u00ikq"
DROPBOX_APP_KEY_SECRET = "1bcaudn17ab9ach"
DROPBOX_APP_MODE = "dropbox"
3. create routes
match '/home/authorize' => 'home#authorize' , :via => [:get,:post] , :as => :dropbox_auth
match '/home/callback' => 'home#callback' , :via => [:get,:post] , :as => :dropbox_callback
4. Logic in cotroller
def authorize
dbsession = DropboxSession.new(DROPBOX_ APP_KEY, DROPBOX_APP_KEY_SECRET)
#serialize and save this DropboxSession
session[:dropbox_session] = dbsession.serialize
filename = params[:file].original_ filename
extension = filename.split('.').last
tmp_file = "#{Rails.root}/tmp/#{filename} "
File.open(tmp_file, 'wb') do |f| f.write params[:file].read end
#pass to get_authorize_url a callback url that will return the user here
redirect_to dbsession.get_authorize_url url_for(:action => 'callback',:file => tmp_file)
end
def callback
dbsession = DropboxSession.deserialize( session[:dropbox_session])
# debugger
if params[:not_approved].blank?
dbsession.get_access_token
dbsession = DropboxSession.deserialize( current_user.dropbox_session)
# create the dropbox client object
client = DropboxClient.new(dbsession, DROPBOX_APP_MODE)
data = File.read(params[:file])
# client.put_file("test.png", data)
client.put_file(params[:file], data)
session[:dropbox_session] = dbsession.serialize
current_user.update_ attributes(:dropbox_session => session[:dropbox_session])
session.delete :dropbox_session
flash[:success] = "You have successfully authorized with dropbox."
end
redirect_to root_url
end
No comments:
Post a Comment