Dropbox

Difference between version 0 and 1 - Previous - Next
** 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.
   1. Go to the "App Console" at https://www.dropbox.com/developers/apps
   1. Click on "Create app".   11. What type of app do you want to create? Select "Dropbox API app".
   11. What type of data does your app need to store on Dropbox? Select "Files and datastores".
   11. Can your app be limited to its own folder? Select "Naccording to your preference. For this example, choose "Yes - My app only needs access to files alit creadtes."
   11. If you chose "No" to the previous question, Dropbox"
 will also 11ask you what types of files the app needs to access. SFor full access, select "All file types -- My app needs access to a user's full Dropbox."
   11. Provide an app name (for simplicity, just a random string).
   1. After creating the app, find the "OAuth 2" section.
   1. Click on "Generate access token".
   1. You will get the access token (a very long string). Copy this token to your app.

*** Prerequisites ***

You need HTTP and TLS:

 package require http
 package require tls
 http::register https 443 ::tls::socket
*** File Upload ***

Now let's upload some file to your Dropbox. The basic idea is:

   * The base URL for uploading is "https://api-content.dropbox.com/1/files_put/auto", everything after that is the path into your Dropbox. If you chose that the app is limited to its own folder, this path is "chrooted" to the "/Apps/<app-name>" folder in your Dropbox.
   * Note that the file name must be URL-encoded, e.g., space characters must be replaced with %20.
   * Send an HTTP PUT-request for the desired URL with the file contents as body.
   * The HTTP headers must include a line "Authorization: Bearer ${access_token}", where access_token is copied from above.

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 ***
To download a fFile, 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 foldver.txt"n from my Public directory. Note that the fsile name must be URL-encoded.r:
   * The base URL for downloading is "https://api-content.dropbox.com/1/files/auto". (Same rules as above.)
   * Send an HTTP GET-request for the desired URL.
   * The HTTP headers must include a line "Authorization: Bearer ${access_token}", where access_token is copied from above.

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 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
GetIf the fdownload is successful, the cserver respontentds with ha 200 statp::us code, and the HTTP body will contain $the file coknten.ts:
*** Filset Uplcoantents [http::data ***$token]
FSome file upmetadata is alsoa returned in the server response's "x-dropbox-metadata" headerly, asgain sin JSON formple:at.
*** seAut horizaccess_tioken "<copy a***

Thindgs pastre fmore complex abif yove>"
u swould like to basccess "https://api-contmeont.de else's Dropbox.com/1/files_put/auto"
 sThet url "${base}${file}"
r muset contbent "<fredilre contents>"
d seto cDrontpbox' Wenb sitLengt (which [has tringo lbength $cdontent]
 outsidet Tconl) tentTypeo "applicautihon/binary"
 sizet access_tok. Then "<cauthopy rizationd paste frlomw above>"
 iset headerscribed [lisn the Cdontcument-Lengath $ciont rentLferengthced Hbelostw.

*** api-contReferences ***

   * https://www.dropbox.com/developers/core/docs ADocumenthorization "Bof thea Dreropbox $access_tCokren"] API.
 set token [* http::g//tools.ietuf.org/html $u/rlfc6749 -Theaders $OAutheaders -me2.0 Authod PUT -rizatype $ciontentType -queFry $contament]
 http::wait $torken.

======