Shimrod
Shimrod

Reputation: 3205

Access to dropbox/google drive

In my ruby on rails application, I would like to be able to read (only read) files from a directory in my own dropbox.

All the tutorials I have found are to authorize access to the visitor's dropbox, and so require to login to dropbox using their oauth login page.

Is there a way to do it by using my credentials I'd save in a file in my application (and so without needing to log myself manually) ?

I'd also like to be able to do it from google drive.

Thanks !

Upvotes: 1

Views: 1010

Answers (1)

Serjik
Serjik

Reputation: 10931

I've followed the following steps and I've read/write to my Dropbox.

Title: Using Dropbox with Ruby On Rails on Heroku

Objective: Heroku does not offer a persist storage and suggests amazon s3 which needs a credit card to register and use it. So Dropbox may be a good replacement at least for training and development level.

Steps: 1. Install sdk command: gem install Dropbox-sdk link: https://www.Dropbox.com/developers-v1/core/sdks/ruby

  1. Create a Dropbox accout if you don't have one link: https://www.Dropbox.com action: create an account

  2. Create an App on Dropbox platform link: https://www.Dropbox.com/developers/apps action: Specify a name for you app and you will given App Key and App Secret remarks: App can have access to whole Dropbox or just a specific folder

  3. Try this basic tutorial to test what you can do link: https://www.Dropbox.com/developers-v1/core/start/ruby action: a.replace 'INSERT_APP_KEY' and 'INSERT_APP_SECRET' with your App keys b.Execute ruby script c.Browse given link to authorize and generate access token d.Copy and paste the code on the script console and continue caution: The script is trying to load a local file first, so be sure you create it on proper path Execution steps: a.Authenticate b.Upload file c.Download file and write it to local

  4. You could generate access token on your app home and use it instead of everytime generating it with APP_KEY & APP_SECRET

  5. To Use Dropbox with rails (CarrierWare) link: https://github.com/robin850/carrierwave-Dropbox steps: 6a. include gem 'carrierwave-Dropbox' in your Gemfile 6b. run 'bundle install' 6c. run rake Dropbox:authorize APP_KEY=app_key APP_SECRET=app_secret ACCESS_TYPE=Dropbox|app_folder

6d. set appropriate settings in your ImageUploader file (CustomNameUploader)

        class ImageUploader < CarrierWave::Uploader::Base
        storage :Dropbox

        def initailize
                CarrierWave.configure do |config|
                    ...
                    # Dropbox settings
                    ...
                end
        end
        end

7. If your are on a source control it will be better choice to set values as env vars and then use them instead. The link shows how to set or persist environment variables in ubuntu. link: https://help.ubuntu.com/community/EnvironmentVariables

  1. On production (heroku) set environment vars like follows: usage: heroku config:set ACCESS_TOKEN_SECRET='your_app_access_token_secret' link: https://devcenter.heroku.com/articles/config-vars

  2. It will be a good practice to create carrierwave.rb file in config/initializers and place all setting in that file also it can be set conditionally for production and development

  3. Beware that Dropbox may be slow and you will get application error, so try it with smaller files and load them with pagination if they are too many.

This is a published link on linkedin: https://www.linkedin.com/pulse/using-dropbox-ruby-rails-heroku-serjik-isagholian?trk=prof-post

Upvotes: 1

Related Questions