Connect to Dropbox template
  • AI Chat
  • Code
  • Report
  • Spinner

    Connect to Dropbox

    This workspace contains instructions and sample code to connect to and access files on Dropbox. This guide assumes that you already have a Dropbox account.

    Setup

    Sign in to your Dropbox account, head over to https://www.dropbox.com/developers/apps and click "Create app".

    In the app creation form:

    • Select the "Scoped access" API.
    • Choose the type of access you need.
    • Specify a name for your app.

    After the app is created, head over to Permissions tab and select the correct access scopes for what you intend to do. For example, if you only want to use Workspace to programmatically read content from Dropbox, files.metadata.read and files.content.read is enough. Make sure to click "Submit" in the footer to persist the changes.

    Go back to the settings tab, and in the OAuth 2 section click "Generate" to generate an access token. Copy the access token to your clipboard.

    Securely store the access token as an environment variable in Workspace:

    • In the Workspace editor, open the 'Environment' side panel and click on "+" next to "Environment variables"
    • Set Name to DROPBOX_ACCESS_TOKEN
    • In Value paste the access token that you copied to your clipboard in the previous step.
    • Set the "Environment Variable Set Name" to something meaningful, e.g. "Dropbox Access Token"
    • Click "Create", "Next" and finally, "Connect". Your workspace session will restart, and DROPBOX_ACCESS_TOKEN will now be available as an environment variable in your workspace session.

    You're now ready to switch to Python to access the files!

    Use Python to list files

    Import packages

    dropbox is the official Python package by the creators of Dropbox to create, configure and manage your Dropbox account. It's already installed by default, so we only have to import it, along with the os package that we need to read out the environment variable:

    import os
    from dropbox import Dropbox

    Create dropbox object

    Create a Dropbox client object with the access token.

    try:
        dbx = Dropbox(os.environ["DROPBOX_ACCESS_TOKEN"])
    except:
        print("Token error")

    List files

    Use methods on the dbx object to list all the files in the root folder. Feel free to adjust this code for your use case!

    files = dbx.files_list_folder(path = "").entries
    for file in files:
        print(file.name)

    Going deeper

    Other than listing files, dropbox allows you to download and upload files, manage files, folders, sharing, etc. Consult the dropbox documentation to learn more.