cancel
Showing results forĀ 
ShowĀ Ā onlyĀ  | Search instead forĀ 
Did you mean:Ā 
Announcements
Share your feedback on the Document Scanning Experience in the Dropbox App right here.

Discuss Dropbox Developer & API

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

Not able to get team folders or files using python sdk

Not able to get team folders or files using python sdk

GopalN
Explorer | Level 4
Go to solution

Python SDK,
Not Able to get files and folders 
where I'm using dropbox business API access token

output = dbx.DropboxTeam(_dropbox_token).as_admin(ng_member_id).files_list_folder("").entries
print(output)

Trying this as suggested from the doc.
help me to do it 
Trying to get files from a folder of folders and download the files.

17 Replies 17

Greg-DB
Dropbox Staff
Go to solution

@phermans Thanks for the feedback!

 

I see you're currently getting the error:

Invalid select user id format

That's referring to the value you're passing to identify the particular user account from the team to operate on, which in your case is via the as_admin method to which you're supplying your variable '_member_id'. So, it appears your '_member_id' value is not valid. That value should be a "team member ID" (which starts with "dbmid:"). You can get the team member IDs from a number of places on the API, such as team_members_get_info and team_members_list/team_members_list_continue. It's returned in the TeamMemberProfile.team_member_id field.

 

Also, note that API calls for teams using the team space configuration default to the "team member folder", not the "team space". You can access the team space though by setting the relevant root. You can do so via the with_path_root method. (You mentioned reviewing some guides, but just in case you missed this one, the Team Files Guide would be the most relevant to this part.) 

 

So, putting that all together, it sounds like this is what you're looking to do:

 

import dropbox

_dropbox_token = "<ACCESS_TOKEN>"
admin_email = "<EMAIL_ADDRESS>"

dbx_team = dropbox.DropboxTeam(_dropbox_token)

_member_id = dbx_team.team_members_get_info([dropbox.team.UserSelectorArg.email(admin_email)])[0].get_member_info().profile.team_member_id

dbx_admin = dbx_team.as_admin(_member_id)

root_namespace_id = dbx_admin.users_get_current_account().root_info.root_namespace_id

dbx_admin_team_root = dbx_admin.with_path_root(dropbox.common.PathRoot.root(root_namespace_id))

listing = dbx_admin_team_root.files_list_folder('')
for entry in listing.entries:
    print(entry.name)

while listing.has_more:
    listing = dbx_admin_team_root.files_list_folder_continue(listing.cursor)
    for entry in listing.entries:
        print(entry.name)

Hope this helps! 

 

phermans
Explorer | Level 3
Go to solution

@Greg-DB Thanks again! Yes it helped a lot. I accidentally forgot to include the dbmid: part......silly me.  I really appreciate your help. I will give it a go a bit later today and hopefully make more progress.

 

Thanks again!

phermans
Explorer | Level 3
Go to solution

@Greg-DB , I got that part working, but don't totally understand one part, hoping I missed it in the docs somewhere, or that there is a simple explanation.  In the code you posted, there was both a for loop and a while loop...

 

for entry in listing.entries:
    print(entry.name)

while listing.has_more:
    listing = dbx_admin_team_root.files_list_folder_continue(listing.cursor)
    for entry in listing.entries:
        print(entry.name)

Not sure I understand why i need both of those. Seems like the initial for loop only gets some of the folders/files.....then the while keeps asking for more....but if that is correct, why even bother with the first for loop, why not just use the while listing.has_more loop?  Wouldn't that just keep going until all the files/folders were returned, and in this case, had their names printed?

 

Thanks for clarifying.

 

Greg-DB
Dropbox Staff
Go to solution

@phermans In order to scale to folder listings of any size, the list_folder interface is paginated. The API may return the results across multiple pages, and each page can contain multiple files/folders. You need to implement calls to both files_list_folder and files_list_folder_continue to make sure you can receive all of the entries.

 

The first for loop iterates over the results on the first page, from files_list_folder; the while loop then has the app make additional calls to files_list_folder_continue as needed, and the second for loop iterates over the results from those subsequent pages, if any, from files_list_folder_continue.

 

Check out the files_list_folder documentation for more information on using this functionality.

mukesh111
New member | Level 2
Go to solution
output = dropbox.DropboxTeam(TOKEN).with_path_root(dropbox.common.PathRoot.root(team_space_namespace_id)).as_admin(member_id).files_list_folder("").entries

what is the team_space_namespace_id here? I use a 10-digit number

 

Traceback (most recent call last):
File "/home/mukesh/Documents/automation/app_test/dropbox/test_dropbox.py", line 20, in <module>
output = dropbox.DropboxTeam(TOKEN).with_path_root(dropbox.common.PathRoot.root(team_space_namespace_id)).as_admin(member_id).files_list_folder("").entries
File "/home/mukesh/Documents/BOVIWALK/projects/pose_estimation/boviwalk-ai/venv/lib/python3.10/site-packages/dropbox/base.py", line 2145, in files_list_folder
r = self.request(
File "/home/mukesh/Documents/BOVIWALK/projects/pose_estimation/boviwalk-ai/venv/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 326, in request
res = self.request_json_string_with_retry(host,
File "/home/mukesh/Documents/BOVIWALK/projects/pose_estimation/boviwalk-ai/venv/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 476, in request_json_string_with_retry
return self.request_json_string(host,
File "/home/mukesh/Documents/BOVIWALK/projects/pose_estimation/boviwalk-ai/venv/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 596, in request_json_string
self.raise_dropbox_error_for_resp(r)
File "/home/mukesh/Documents/BOVIWALK/projects/pose_estimation/boviwalk-ai/venv/lib/python3.10/site-packages/dropbox/dropbox_client.py", line 643, in raise_dropbox_error_for_resp
raise PathRootError(request_id, err)
dropbox.exceptions.PathRootError: PathRootError('b0bc4fd1e82647deae8616b37255c2cc', PathRootError('invalid_root', TeamRootInfo(home_namespace

ЗГравко
Legendary | Level 20
Go to solution

Hi @mukesh111,

Take a look on Greg's post on this page top. There is pretty clear where 'team_space_namespace_id' comes from. Try reproduce something similar. :winking_face: One more thing, at present access token is 'short lived' only. If you need long term access, refresh token may be object of consideration from your side.

Hope this helps.

mukesh111
New member | Level 2
Go to solution

Thanks for the reference it helped. I see that there are solutions to download files are recreate the whole structure and downloading them. Is there a single function that can handle downloading a complete directory

ex: I want to download a whole FolderA with (subfolder1, subfolder2, subfolder3)

I could only find the below for files is there for folders by any chance?:

dbx_admin_team_root.files_download_to_file()

ЗГравко
Legendary | Level 20
Go to solution

Hi again @mukesh111,

You can download a folder as zip file. You can use the zip stream using files_download_zip and unzip the content on fly or save it as file using files_download_zip_to_file. :winking_face:

Good luck.

Need more support?
Who's talking

Top contributors to this post

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