In my Python class, I have a function that generates a csv file, uploads this file to Dropbox, and then attempts to generate a shared link to the uploaded file with an expiration time of 5 minutes.
I am attempting to follow the example provided here; my code is below:
# Helper upload function
def upload_file(self, dbx, file_from, file_to):
"""upload a file to Dropbox using API v2
"""
with open(file_from, 'rb') as f:
dbx.files_upload(f.read(), file_to, mode=WriteMode('overwrite'))
# Target function
def generate_expiring_dropbox_link(self, local_path, dbx_path):
dbx = dropbox.Dropbox(self.temp_dropbox_token)
expires = datetime.datetime.now() + datetime.timedelta(minutes=5)
requested_visibility = dropbox.sharing.RequestedVisibility.team_only
desired_shared_link_settings = dropbox.sharing.SharedLinkSettings(requested_visibility=requested_visibility,
expires=expires)
# open the file and upload it
self.upload_file(dbx, local_path, dbx_path)
shared_link_metadata = dbx.sharing_create_shared_link_with_settings(path=dbx_path, settings=desired_shared_link_settings)
return shared_link_metadata
I consistently get an API error related to the shared link settings:
dropbox.exceptions.ApiError: ApiError('************', CreateSharedLinkWithSettingsError('settings_error', SharedLinkSettingsError('invalid_settings', None)))
I can't find much documentation on this; has anyone encountered it/found a solution? Just wondering if there's a python-based fix or if wrapping an HTTP request is a better bet. I'm using Python 3.6 and Dropbox 8.9.0.
The Dropbox API expects UTC time. You're supplying local time, which can be in the past relative to UTC time. If so, the API will reject the settings as the expiration can't be in the past.
So, instead of:
expires = datetime.datetime.now() + datetime.timedelta(minutes=5)
Do:
expires = datetime.datetime.utcnow() + datetime.timedelta(minutes=5)