cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know what we learned at IBC? Check out our learnings on media, remote working and more right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to define dropbox file paths in python for the API ???

How to define dropbox file paths in python for the API ???

cjr_lyris
Explorer | Level 3

I simply cannot get this script to provide the the correctly formatted file path to the dropbox API, I keep getting the error that the filepath doesn't exist.

And I can't find a definitive answer on how the API needs to receive the path.

Also, what is the difference in paths between a file in the personal user area, and a file in the shared team area?


I keep getting this error:

ApiError(70782f83ba81470284d6e6905b9e828e, CreateSharedLinkWithSettingsError(path, LookupError(malformed_path, None)))

 

 

Any help apreciated!!


import dropbox
import os
from subprocess import Popen, PIPE

ACCESS_TOKEN = 'XXXXX'

def get_selected_file_in_finder():
# AppleScript to get the selected file in Finder
applescript = '''
tell application "Finder"
set theSelection to selection as alias list
set theItem to item 1 of theSelection
return POSIX path of (theItem as text)
end tell
'''
process = Popen(['osascript', '-e', applescript], stdout=PIPE)
stdout, stderr = process.communicate()
return stdout.strip().decode('utf-8')

def get_dropbox_link(file_path, dbx):
dropbox_root = "XXXXX"
personal_folder = "XXXXX"

if file_path.startswith(dropbox_root):
relative_path = file_path[len(dropbox_root):]
relative_path = relative_path if relative_path.startswith('/') else '/' + relative_path

if relative_path.startswith(personal_folder):
dropbox_path = relative_path[len(personal_folder):] # Strip the personal folder name
else:
dropbox_path = relative_path # Team space path
else:
os.system(f'osascript -e \'display dialog "The selected file is not in the Dropbox folder."\'')
return None

os.system(f'osascript -e \'display dialog "Dropbox path: {dropbox_path}"\'') # Added logging for Dropbox path

try:
shared_link_metadata = dbx.sharing_create_shared_link_with_settings(dropbox_path)
os.system(f'osascript -e \'display dialog "API response: {shared_link_metadata}"\'') # Added logging for API response
return shared_link_metadata.url.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=0", "")
except dropbox.exceptions.ApiError as err:
if isinstance(err.error, dropbox.sharing.CreateSharedLinkWithSettingsError) and \
err.error.is_shared_link_already_exists():
os.system('osascript -e \'display dialog "Shared link already exists, retrieving the existing link."\'')
shared_links = dbx.sharing_list_shared_links(path=dropbox_path, direct_only=True)
if shared_links.links:
return shared_links.links[0].url.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=0", "")
os.system(f'osascript -e \'display dialog "Error getting Dropbox link: {err}"\'')
return None
except Exception as e:
os.system(f'osascript -e \'display dialog "Unexpected error: {e}"\'')
return None

def main():
dbx = dropbox.Dropbox(ACCESS_TOKEN)

file_path = get_selected_file_in_finder()

if not file_path:
os.system('osascript -e \'display dialog "No file selected in Finder."\'')
return

os.system(f'osascript -e \'display dialog "Selected file path: {file_path}"\'') # Added logging for file path

download_link = get_dropbox_link(file_path, dbx)

if download_link:
os.system(f'osascript -e \'display dialog "Dropbox download link: {download_link}"\'')
else:
os.system('osascript -e \'display dialog "Failed to generate Dropbox download link."\'')
os.system(f'osascript -e \'display dialog "Failed to generate Dropbox download link for file: {file_path}"\'') # Added logging for failure

if __name__ == '__main__':
main()

2 Replies 2

Greg-DB
Dropbox Staff

A 'malformed_path' error like this indicates that the supplied path wasn't in the expected format. To troubleshoot that, can you print out and share the path value you're sending? It looks like that would be the value of the 'dropbox_path' variable in your code.

 

For information on the difference between the member folder and the team space, and how to access them, check out the Team Files Guide.

Здравко
Legendary | Level 20

Hi @cjr_lyris,

Does your 'personal_folder' ends with slash or not? 🧐 Incorrect decision may lead to in malformed path and such an error accordingly. If I have to bet, that's it. 😉 Follow Greg's advice to confirm it or reject.

Hope this gives direction.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Здравко Legendary | Level 20
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?