Dropbox: Use the API in 5 Simple Steps

07.01.2020 yahe code

A few weeks ago I wrote about the new cryptographic basis of the Shared-Secrets service. What I did not write about was that one user asked if a file-sharing option could be added. I declined because sharing files is nothing that the service is meant to be there for. But I tried whether sharing files would be possible.

To share the files I needed a place to store them, but I did not feel like storing them on the actual server. So I searched for an easy file storage service. The first service that came to my mind was Dropbox. I had never used Dropbox nor the Dropbox API but felt that it should not be all too difficult. But I did not know how easy it actually was to use the Dropbox API. Because of this simplicity I decided to write a short blog post about it. So here are 5 simple steps to use the Dropbox API...

1. Register your Account

First of all you need to register a Dropbox account. For this you should use a valid e-mail address that you have access to because you will have to verify that e-mail address later on.

Dropbox Account Registration

2. Verify your E-Mail Address

After logging in with your new account you can visit the app creation page where you are requested to verify your e-mail address. You have to proceed with the verification in order to create your own Dropbox App.

Dropbox E-Mail Address Verification

3. Create your Dropbox App

Now that you have verified your e-mail address you can visit the app creation page again to create the actual app. Select to use the Dropbox API, to store the files in a separate App folder, choose a name for your App and you are ready to go.

Dropbox App Creation

4. Retrieve Your Access Token

When you have created the app the app details will be shown to you. Scroll down a bit and you will find a button labeled "Generate" in the "OAuth 2" section that will generate your individual access token. The Access Token displayed below the "Generated access token" heading is needed to identify your Dropbox Account.

Dropbox App 1 Dropbox App 2

5. Use the Dropbox API

Using the Dropbox API itself is relatively simple as most actions can be done with a single REST API call. Here are some PHP examples that illustrate the usage of the Dropbox API. First of all we define the Access Token which is needed by all API calls:

  // see https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/
  define("DROPBOX_ACCESS_TOKEN", "YOUR DROPBOX ACCESS TOKEN");

Now, in order to store a given string ($content) in Dropbox we can use the following function. On failure it returns NULL and on success it returns a random identifier through which the content can be retrieved again:

  function dropbox_upload($content) {
    $result = null;

    // store content in memory
    if ($handler = fopen("php://memory", "rw")) {
      try {
        if (strlen($content) === fwrite($handler, $content)) {
          if (rewind($handler)) {
            // get random filename
            $filename = bin2hex(openssl_random_pseudo_bytes(32, $strong_crypto));

            if ($curl = curl_init("https://content.dropboxapi.com/2/files/upload")) {
              try {
                $curl_headers = ["Authorization: Bearer ".DROPBOX_ACCESS_TOKEN,
                                 "Content-Type: application/octet-stream",
                                 "Dropbox-API-Arg: {\"path\":\"/$filename\"}"];

                curl_setopt($curl, CURLOPT_HTTPHEADER,     $curl_headers);
                curl_setopt($curl, CURLOPT_PUT,            true);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST,  "POST");
                curl_setopt($curl, CURLOPT_INFILE,         $handler);
                curl_setopt($curl, CURLOPT_INFILESIZE,     strlen($content));
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

                $response = curl_exec($curl);
                if (200 === curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {
                  $result = $filename;
                }
              } finally {
                curl_close($curl);
              }
            }
          }
        }
      } finally {
        fclose($handler);
      }
    }

    return $result;
  }

To retrieve the stored content the following function can be used. It requires the random identifier ($filename) as the input parameter:

  function dropbox_download($filename) {
    $result = null;

    if ($curl = curl_init("https://content.dropboxapi.com/2/files/download")) {
      try {
        $curl_headers = ["Authorization: Bearer ".DROPBOX_ACCESS_TOKEN,
                         "Content-Type: application/octet-stream",
                         "Dropbox-API-Arg: {\"path\":\"/$filename\"}"];

        curl_setopt($curl, CURLOPT_HTTPHEADER,     $curl_headers);
        curl_setopt($curl, CURLOPT_POST,           true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($curl);
        if (200 === curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {
          $result = $response;
        }
      } finally {
        curl_close($curl);
      }
    }

    return $result;
  }

In order to delete the stored content the following function can be used. It again requires the random identifier ($filename) as the input parameter:

  function dropbox_delete($filename) {
    $result = null;

    if ($curl = curl_init("https://api.dropboxapi.com/2/files/delete_v2")) {
      try {
        $curl_fields  = json_encode(["path"  => "/$filename"]);
        $curl_headers = ["Authorization: Bearer ".DROPBOX_ACCESS_TOKEN,
                         "Content-Type: application/json"];

        curl_setopt($curl, CURLOPT_HTTPHEADER,     $curl_headers);
        curl_setopt($curl, CURLOPT_POST,           true);
        curl_setopt($curl, CURLOPT_POSTFIELDS,     $curl_fields);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($curl);
        if (200 === curl_getinfo($curl, CURLINFO_RESPONSE_CODE)) {
          $result = $response;
        }
      } finally {
        curl_close($curl);
      }
    }

    return $result;
  }

And there we have it. With a few lines of code we were able to store, retrieve and delete contents in Dropbox. In my case I also encrypted the content before storing it in Dropbox, something that you should consider as well if you are handling confidential contents. But other than that the shown functions are most of what you will need in the beginning.


Search

Categories

administration (42)
arduino (12)
calcpw (2)
code (37)
hardware (16)
java (2)
legacy (113)
linux (28)
publicity (6)
review (2)
security (60)
thoughts (21)
windows (17)
wordpress (19)