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:
You need HTTP and TLS:
package require http package require tls http::register https 443 ::tls::socket
Now let's upload some file to your Dropbox. The basic idea is:
Assuming that $file is the URL-encoded path to the file in your Dropbox and $content is the data you would like to upload, this then looks like:
set file "/test.txt" set contents "Hello World!" set access_token "<copy and paste from above>" set base "https://api-content.dropbox.com/1/files_put/auto" set url "${base}${file}" 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
If the upload is successful, the server responds with a 200 status code and an HTTP body with some useful information in JSON format.
File download is even easier:
Again assuming that $file is the URL-encoded path to the file in your Dropbox:
set file "/test.txt" set access_token "<copy and paste from above>" 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"] set token [http::geturl $url -headers $headers] http::wait $token
If the download is successful, the server responds with a 200 status code, and the HTTP body will contain the file contents:
set contents [http::data $token]
Some file metadata is also returned in the server response's "x-dropbox-metadata" header, again in JSON format.
Things are more complex if you would like to access someone else's Dropbox. The user must be redirected to Dropbox' Web site (which has to be done outside Tcl) to authorize access. The authorization flow is described in the documentation referenced below.