You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

cjr_lyris's avatar
cjr_lyris
Explorer | Level 3
6 months ago

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

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()

  • Здравко's avatar
    Здравко
    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.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,885 PostsLatest Activity: 6 hours ago
326 Following

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!