Dropbox Python SDK

repository·main·Indexed 20 days ago

https://github.com/dropbox/dropbox-sdk-python

Official Dropbox API Client for Python. This SDK provides a comprehensive interface to manage files, folders, and account settings programmatically. It includes support for individual user accounts via the Dropbox class, administrative tasks via the DropboxTeam class, and OAuth2 authentication through the dropbox.oauth module. The SDK also features a dropbox.async_ module for asynchronous operations and provides detailed error handling for API v2 exceptions, rate limits, and authentication errors.

Tokens
28.7K
Snippets
62
Records
153
Agent score
76%

What's inside dropbox-sdk-python

  1. Use the async module for asynchronous operations

    main

    The dropbox.async_ module provides asynchronous versions of the Dropbox SDK functionality.

    Important Python Version Compatibility:

    • Python 3.5+: You must import from dropbox.async_ because async is a reserved keyword.
    • Python < 3.5: You can import from either dropbox.async or dropbox.async_.
    # For Python 3.5 and newer
    import dropbox.async_
  2. Upgrade to v12.0.0: TLS Certificate Verification

    main

    Starting with version 12.0.0, the SDK no longer provides its own internal CA bundle for verifying TLS connections. Instead, it relies on the requests library, which uses the certifi package for certificate verification.

    If you need to use a custom certificate bundle, you can still provide one using the ca_certs parameter in the Dropbox classes or the create_session function.

    # Example of providing a custom CA bundle
    # (Note: parameter availability depends on the specific Dropbox class used)
    dropox_client = Dropbox(access_token, ca_certs='/path/to/your/cert_bundle.pem')
  3. Install the Dropbox Python SDK

    main

    To use the official Dropbox SDK for Python, ensure you have Python 3.11 or newer installed. Older versions (including Python 2.7 and 3.4 through 3.10) are not supported.

    Via pip

    You can install the SDK directly from PyPI using pip:

    $ pip install dropbox

    From source

    If you need to install from the repository source:

    $ git clone git://github.com/dropbox/dropbox-sdk-python.git
    $ cd dropbox-sdk-python
    $ pip install .

    Important Compatibility Note: You must use v12.0.2 or newer of this SDK to maintain compatibility with Dropbox API servers. Versions older than v12.0.2 stopped working in January 2026 due to API server certificate changes.

  4. Upgrade from v10.X.X to v11.0.0: Import Changes

    main

    In version 11.0.0, the internal structure of the SDK was updated, resulting in the renaming of dropbox.dropbox to dropbox.dropbox_client.

    • If you use direct imports like import dropbox.dropbox.foo, you must update them to import dropbox.dropbox_client.foo.
    • If you use standard imports via the package root (e.g., from dropbox import Dropbox), your code will continue to work without changes because these imports are preserved in dropbox/__init__.py.
    # OLD (v10.X.X)
    import dropbox.dropbox
    client = dropbox.dropbox.Dropbox()
    
    # NEW (v11.0.0+)
    import dropbox.dropbox_client
    client = dropbox.dropbox_client.Dropbox()
    
    # RECOMMENDED (Works in both)
    from dropbox import Dropbox
    client = Dropbox()
  5. Run the Backup and Restore sample app

    main

    To run the Backup and Restore sample application, follow these steps:

    1. Install the Dropbox Python SDK: Ensure the SDK is installed following the official installation instructions.
    2. Configure your Access Token: Open backup-and-restore-sample.py and locate the TOKEN variable. Replace the empty string with your valid Dropbox access token:
      TOKEN = 'YOUR_ACCESS_TOKEN'
    3. Execute the script: Navigate to the example/backup-and-restore directory and run the script using Python.

    Upon successful execution, a file named my-file-backup.txt will appear in your Dropbox account.

    python backup-and-restore-sample.py
  6. Explore Dropbox SDK Python examples

    main

    The repository provides several example applications to demonstrate core SDK functionality. These are categorized into OAuth flows and general file operations:

    OAuth Examples

    • Commandline OAuth Basic: A simple commandline OAuth implementation without a redirect.
    • Commandline OAuth Scopes: Demonstrates commandline OAuth using specific scopes.
    • Commandline OAuth PKCE: Demonstrates commandline OAuth using the PKCE (Proof Key for Code Exchange) flow.

    File Operation Examples

    • Updown: A sample application that uploads the contents of your local Downloads folder to Dropbox.
    • Backup and Restore: A sample application demonstrating how to backup a file and restore previous versions if a file is modified or corrupted.
  7. Manage file properties using PropertyGroup

    main

    Dropbox file properties are organized into PropertyGroup objects. A PropertyGroup is a subset of fields defined by a specific template_id. Properties are always added to a Dropbox file as a PropertyGroup.

    Key components:

    • template_id: A unique identifier for the associated template.
    • fields: A list of PropertyField objects containing the actual property data.

    To update properties on a file, use UpdatePropertiesArg which takes a path and a list of PropertyGroupUpdate objects to apply 'delta' updates.

    # Example conceptual usage for updating properties
    update_arg = UpdatePropertiesArg(
        path="/path/to/file.txt",
        update_property_groups=[
            PropertyGroupUpdate(
                template_id="your_template_id",
                add_or_update_fields=[PropertyField(name="key", value="value")]
            )
        ]
    )
  8. Asynchronous document conversion and metadata extraction (Riviera)

    main

    The Riviera namespace provides asynchronous tools for extracting text, markdown, or metadata from various file formats. These operations return a LaunchResultBase which must be polled using a _check method to retrieve the final result.

    Supported Formats:

    • Markdown Conversion: .binder, .docx, .html, .paper, .pptx, .xlsx, .gsheet, .ods, .pdf.
    • Metadata Extraction: Images (EXIF), Audio/Video (media), PDF, and MS Office.
    • Text Extraction: Word processing (.doc, .docx, .rtf), Presentations (.ppt, .pptx), Spreadsheets (.xls, .xlsx), PDF, and Dropbox docs (.paper, .gdoc, etc.).

    Workflow:

    1. Call the async method (e.g., riviera_get_text_async) with a file_id_or_url.
    2. Receive an async_job_id within the LaunchResultBase.
    3. Poll the status using the corresponding _check method (e.g., riviera_get_text_async_check(async_job_id)).

    Constraints:

    • Source files for markdown conversion must be $\le$ 50 MB.
    • For url variants in text extraction, only Dropbox shared links (www.dropbox.com) are supported.
    # 1. Launch the async job
    launch_result = dbx.riviera_get_text_async(file_id_or_url=my_file_id)
    job_id = launch_result.async_job_id
    
    # 2. Poll for completion
    while True:
        check_result = dbx.riviera_get_text_async_check(job_id)
        if check_result.is_finished:
            print(check_result.result)
            break
  9. Handle AccessError tagged unions

    main

    The AccessError class is a tagged union used when an account lacks permission to access a specific resource. To handle it correctly, first check the specific error type using an is_* method, then retrieve the associated error data using the corresponding get_* method.

    Available error types:

    • invalid_account_type: The current account type cannot access the resource. Returns InvalidAccountTypeError.
    • paper_access_denied: The current account cannot access Paper. Returns PaperAccessError.
    • no_permission: The caller does not have permission to access the resource. Returns NoPermissionError.
    • team_access_denied: The team doesn't have permission to access.
    if error.is_invalid_account_type():
        details = error.get_invalid_account_type()
        # Handle InvalidAccountTypeError
    elif error.is_paper_access_denied():
        details = error.get_paper_access_denied()
        # Handle PaperAccessError
    elif error.is_no_permission():
        details = error.get_no_permission()
        # Handle NoPermissionError