pythondropboxdropbox-apidropbox-sdk

Dropbox Python API - Team Folder Access


I am trying to connect to Dropbox to read in a csv into my python program. I can currently connect, but I can't access the folder I want to access. I configured my app to have all access. When I click on "All Files" in Dropbox Desktop, I currently have two folder there- We'll call them Folder 1 and Folder 2. I want to access a file within Folder 2, which is a Team folder. We do not have Team Space as far as I can tell but I could be wrong ,if that's applicable.

My code looks like this:

try:
    dropbox_file_path = ''

    # connect to dropbox using member id
    dbx = dropbox.DropboxTeam(os.getenv('DROPBOX_TOKEN')).as_user(member_id)

    # list all the files from the folder
    result = dbx.files_list_folder(dropbox_file_path, recursive=False)

    #  print entries
    for entry in result.entries:
        print(entry.name)

except Exception as e:
    print(e)

This code works just fine, but it only prints out the file/folder names within Folder 1. As you can see, my path is an empty string, which should point it to my root. Is there a reason I can't see or access the other folder? I have admin access to Folder 2. It behaves as though that folder doesn't exist and Folder 1 is my root folder. I thought the "All Files" link in dropbox would take me to my root, which would mean Folder 1 and Folder 2 would be children of it, but it doesn't seem to think so. How can I access the correct files?

I get a LookupError when I try to access the file I want since it can't get into that folder.


Solution

  • From your description, it sounds like your team does use the team space configuration.

    By default, API calls operate in the "member folder" of the connected account, not the "team space". You can configure API calls to operate in the "team space" instead though. To do so, you'll need to set the "Dropbox-API-Path-Root" header. You can find information on this in the Team Files Guide.

    In that case, you would do something like:

    root_namespace_id = dbx.users_get_current_account().root_info.root_namespace_id
    dbx = dbx.with_path_root(dropbox.common.PathRoot.root(root_namespace_id))
    
    def handle_entries(entries):
        for entry in entries:
            print(entry)
    
    folder_path = ""
    
    res = dbx.files_list_folder(path=folder_path, recursive=False)
    handle_entries(res.entries)
    while res.has_more:
        res = dbx.files_list_folder_continue(cursor=res.cursor)
        handle_entries(res.entries)