Version 0 of Dropbox

Updated 2014-08-15 16:26:58 by FPX

Access to your personal Dropbox

I recently wanted to download and upload files from my Dropbox. Turns out that this is easy to do in Tcl.

If all you need is access to your own Dropbox, you don't have to implement the authorization process. Instead:

  1. Go to https://www.dropbox.com/ and sign into your account.
  2. Go to the "App Console" at https://www.dropbox.com/developers/apps
  3. Click on "Create app".
    1. Select "Dropbox API app"
    2. Select "Files and datastores"
    3. Select "No - My app needs access to files already on Dropbox"
    4. Select "All file types -- My app needs access to a user's full Dropbox"
    5. Provide an app name (for simplicity, just a random string)
  4. After creating the app, find the "OAuth 2" section
  5. Click on "Generate access token"
  6. You will get the access token (a very long string)

Prerequisites

You need HTTP and TLS:

 package require http
 package require tls
 http::register https 443 ::tls::socket

File Download

To download a file, note its filename within your Dropbox root folder. Prefix it with "https://api-content.dropbox.com/1/files/auto ". Here, I download the default file "How to use the Public folder.txt" from my Public directory. Note that the file name must be URL-encoded.

 set access_token "<copy and paste from above>"
 set file "/Public/How%20to%20use%20the%20Public%20folder.txt"
 set base "https://api-content.dropbox.com/1/files/auto"
 set url "${base}${file}"
 set headers [list Host api-content.dropbox.com Authorization "Bearer $access_token"]

Now just use the http command to download the file:

 set token [http::geturl $url -headers $headers]
 http::wait $token

Get the file content with http::data $token.

File Upload

File upload is nearly as simple:

 set access_token "<copy and paste from above>"
 set base "https://api-content.dropbox.com/1/files_put/auto"
 set url "${base}${file}"
 set content "<file contents>"
 set contentLength [string length $content]
 set contentType "application/binary"
 set access_token "<copy and paste from above>"
 set headers [list Content-Length $contentLength Host api-content.dropbox.com Authorization "Bearer $access_token"]
 set token [http::geturl $url -headers $headers -method PUT -type $contentType -query $content]
 http::wait $token