Dropbox /delta ignoring cursor

399 views Asked by At

I'm trying to list files on Dropbox for Business.

The Dropbox Python SDK does not support Dropbox for Business so I'm using the Python requests module to send POST requests to https://api.dropbox.com/1/delta directly.

In the following function there are repeated calls to Dropbox /delta, each of which should get a list of files along with a cursor.

The new cursor is then sent with the next request to get the next list of files.

BUT it always get the same list. It is as though Dropbox is ignoring the cursor that I am sending.

How can I get Dropbox to recognise the cursor?

def get_paths(headers, paths, member_id, response=None):
    """Add eligible file paths to the list of paths.

    paths is a Queue of files to download later
    member_id is the Dropbox member id
    response is an example response payload for unit testing
    """

    headers['X-Dropbox-Perform-As-Team-Member'] = member_id
    url = 'https://api.dropbox.com/1/delta'
    has_more = True
    post_data = {}

    while has_more:
        # If ready-made response is not supplied, poll Dropbox
        if response is None:
            logging.debug('Requesting delta with {}'.format(post_data))
            r = requests.post(url, headers=headers, json=post_data)
            # Raise an exception if status is not OK
            r.raise_for_status()

            response = r.json()

            # Set cursor in the POST data for the next request
            # FIXME: fix cursor setting
            post_data['cursor'] = response['cursor']

        # Iterate items for possible adding to file list [removed from example]

        # Stop looping if no more items are available
        has_more = response['has_more']

        # Clear the response
        response = None

The full code is at https://github.com/blokeley/dfb/blob/master/dfb.py

My code seems very similar to the official Dropbox blog example, except that they use the SDK which I can't because I'm on Dropbox for Business and have to send additional headers.

Any help would be greatly appreciated.

1

There are 1 answers

10
user94559 On BEST ANSWER

It looks like you're sending a JSON-encoded body instead of a form-encoded body.

I think just change json to data in this line:

r = requests.post(url, headers=headers, data=post_data)

EDIT Here's some complete working code:

import requests

access_token = '<REDACTED>'
member_id = '<REDACTED>'

has_more = True
params = {}
while has_more:
    response = requests.post('https://api.dropbox.com/1/delta', data=params, headers={
        'Authorization': 'Bearer ' + access_token,
        'X-Dropbox-Perform-As-Team-Member': member_id
    }).json()

    for entry in response['entries']:
        print entry[0]

    has_more = response['has_more']
    params['cursor'] = response['cursor']