Ruby client library for Dropbox API v2
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
Library for communicating with Dropbox API v2.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install dropbox_api
Unfortunately, the documentation at RubyDoc.info is disrecommended because it lacks some nice features that have been added with YARD plugins:
Clientclass, just as you will use them.
Dropbox uses OAuth, in order to use this library from your application you'll have to get an authorization code.
Once you have it, just pass it on client initialization:
DropboxApi::Client.new("VofXAX8D...") #=> #<:client ...>
Or set it as an ENV variable called
DROPBOX_OAUTH_BEARER, for example:
ENV["DROPBOX_OAUTH_BEARER"] = "VofXAX8D..." DropboxApi::Client.new #=> #<:client ...>
Note that setting an ENV variable is only a feasible choice if you're only using one account.
The easiest way to obtain an access token is to get it from the Dropbox website. You just need to log in to Dropbox and refer to the developers section, go to My apps and select your application, you may need to create one if you haven't done so yet.
Under your application settings, find section OAuth 2. You'll find a button to generate an access token.
You can obtain an authorization code with this library:
authenticator = DropboxApi::Authenticator.new(CLIENT_ID, CLIENT_SECRET) authenticator.authorize_url #=> "https://www.dropbox.com/..."
Now you need to open the authorization URL in your browser,
authorize the application and copy your code.
auth_bearer = authenticator.get_token(CODE) #=> #<:accesstoken ...>` auth_bearer.token #=> "VofXAX8D..."
Keep this token, you'll need it to initialize a
This is what many web applications will use. The process is described in Dropbox's OAuth guide.
If you have a Rails application, you might be interested in this setup guide.
Once you've initialized a client, for example:
client = DropboxApi::Client.new("VofXAX8D...") #=> #<:client ...>
You can perform an API call like this:
result = client.list_folder "/sample_folder" #=> #<:results::listfolderresult> result.entries #=> [#<:metadata::folder>, #<:metadata::file>] result.has_more? #=> false
The instance of
Clientwe've initialized is the one you'll be using to perform API calls. You can check the class' documentation to find all available endpoints.
If you need to upload files larger than 150MB the default
#uploadendpoint won't work. Instead, you need to start a upload session and upload the file in small chunks.
To make this easier, the method
upload_by_chunkswill handle this for you, example:
client = DropboxApi::Client.new("VofXAX8D...") #=> #<:client ...> File.open("large_file.avi") do |f| client.upload_by_chunks "/remote_path.txt", f end
Check out the method documentation to find out all available options.
It has official support for Ruby versions
After checking out the repo, run
bin/setupto install dependencies. Then, run
bin/consolefor an interactive prompt that will allow you to experiment.
I recommend you to use a test account other than your main one.
We use VCR to record the HTTP calls to Dropbox, however we sometimes need to regenerate the cassettes. Let's take
list_folderas an example to show what would be the procedure to do so:
Manually delete the existing cassettes in
Run the task to build the scaffolding in your Dropbox account so the tests will pass. If it doesn't exist you may need to write it yourself, check the
DropboxScaffoldBuilderclass to find all existing scaffold builders.
DROPBOX_OAUTH_BEARER=YOUR_AUTH_BEARER rake test:build_scaffold[list_folder]
Run the tests and the cassettes will be written:
DROPBOX_OAUTH_BEARER=YOUR_AUTH_BEARER rspec spec/endpoints/files/list_folder_spec.rb
The OAuth bearer shouldn't have been recorded in the cassette and it should've been filtered. However, you may want to double check before pushing your updates to Github.
Tip: You can simply run
export DROPBOX_OAUTH_BEARER=YOUR_AUTH_BEARERat the beginning of your work session so you don't need to prefix it in every command line.
Any help will be much appreciated. The easiest way to help is to implement one or more of the endpoints that are still pending. To see how the endpoints are implemented, check out the