How to Download a Dropbox for Business File Using REST and Python

9 November 2018 / REST

Do you need to download a specific file from Dropbox for Business? As is the case for many businesses, if your Dropbox has thousands of files, it takes a significant amount of time to synchronize your Dropbox client. That's where the REST API can help you. Knowing where a file is located, you can use the API to quickly download a file without waiting for Dropbox to synchronize with the server.

Prerequisites

This guide assumes that you are using Dropbox for Business, are familiar with Python, and have a Linux environment. As of May 2018, the latest Dropbox API for both standard and business users is v2, and v1 has been officially
deprecated.

We're going to use Python in this tutorial, but you can easily adapt the code to other languages. This guide assumes that you have a Dropbox administrator account, have already created a development Dropbox app for your account, and have generated an access token. For information on how to do this, visit this guide.

Dropbox Team Member ID

You will need the team member ID of the Dropbox user that hosts the files. The only way to retrieve this value is through the REST API. We can quickly find this value by doing the following cURL command:

curl -X POST https://api.dropboxapi.com/2/team/members/list \
              --header "Authorization: Bearer ACCESS_TOKEN_HERE" \
              --header "Content-Type: application/json" \
              --data "{\"limit\": 100,\"include_removed\": false}" | python -m json.tool

To make it easier to read, the response is piped through the Python json.tool module. The final output is a JSON list of all your Dropbox users. Increase the limit parameter if you have more than 100 users.

A sample response looks like this. Look for the team_member_id value. We will need it for the final code.

{
"cursor": "AACT56hgxeNMULpQfcouDHlr3SbOXosUMKekNVcJJEQeAmBUgEgWDPddGi6h2ylaAenI0meUgJ3ETAByKDRGBNcA",
"has_more": false,
"members": [
    {
        "profile": {
            "account_id": "dbid:AAJxvKgqjXddQwY8WQYi_liX3HqkRFp6wVmU",
            "email": "example@example.com",
            "email_verified": true,
            "groups": [
            ],
            "joined_on": "2018-07-15T17:36:34Z",
            "member_folder_id": "2918372",
            "membership_type": {
                ".tag": "full"
            },
            "name": {
                "abbreviated_name": "EE",
                "display_name": "Example Example",
                "familiar_name": "Example",
                "given_name": "Example",
                "surname": "Example"
            },
            "status": {
                ".tag": "active"
            },
            "team_member_id": "dbmid:AAC-0P5BoyunnawA_wkvkx1_xNJ90qPFSU5"
        },
        "role": {
            ".tag": "member_only"
        }
    }

Breakdown of Files Download HTTP Request

We are going to make a POST request to the https://content.dropboxapi.com/2/files/download resource. This REST endpoint will accept headers to select the Dropbox user and specify the file's absolute path. Here is the breakdown of each header:

Manuel Figueroa

Full Stack Developer

Thank you for reading my blog. Please feel free to contact me or read my other content.

Manuel Figueroa