Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
Hi, I'm trying to check if a user has access to any folders. My code at the moment is printing 'User has at least one shared folder' for everyone, but I'm not sure if that's correct. Could someone point me in the right direction please? I'm using Python, and this is my code:
dbx = dropbox.DropboxTeam(oauth2_refresh_token=REFRESH_TOKEN, app_key=APP_KEY, app_secret=APP_SECRET).as_user(team_member_id)
shared_folders = dbx.sharing_list_folders(limit=1)
if shared_folders == None:
print('User has no shared folders')
else:
print('User has at least one shared folder')
[Cross-linking for reference: https://stackoverflow.com/questions/73558139/check-if-a-user-has-access-to-any-shared-folders-in-dro... ]
Yes, for the Python SDK, using the sharing_list_folders/sharing_list_folders_continue methods is the right way to list the shared folders that a user account has access to. The Dropbox API doesn't offer a call just for returning a boolean indicating whether or not an account has any shared folders, so listing and counting them would be the right solution.
Note however that the sharing_list_folders method returns a ListFoldersResult, which is not itself the list of shared folders for that result page. That would be in ListFoldersResult.entries. So, you would instead need to do something like `if len(shared_folders.entries) == 0:` instead.
Also while the `limit` parameter sets a maximum page size, it doesn't technically guarantee a minimum page size, so to be safe it would be best to implement the pagination as documented and check the whole list, like this:
shared_folders = []
shared_folders_res = dbx.sharing_list_folders()
shared_folders += shared_folders_res.entries
while shared_folders_res.cursor:
shared_folders_res = dbx.sharing_list_folders_continue(cursor=shared_folders_res.cursor)
shared_folders += shared_folders_res.entries
if len(shared_folders) == 0:
print('User has no shared folders')
else:
print('User has at least one shared folder')
[Cross-linking for reference: https://stackoverflow.com/questions/73558139/check-if-a-user-has-access-to-any-shared-folders-in-dro... ]
Yes, for the Python SDK, using the sharing_list_folders/sharing_list_folders_continue methods is the right way to list the shared folders that a user account has access to. The Dropbox API doesn't offer a call just for returning a boolean indicating whether or not an account has any shared folders, so listing and counting them would be the right solution.
Note however that the sharing_list_folders method returns a ListFoldersResult, which is not itself the list of shared folders for that result page. That would be in ListFoldersResult.entries. So, you would instead need to do something like `if len(shared_folders.entries) == 0:` instead.
Also while the `limit` parameter sets a maximum page size, it doesn't technically guarantee a minimum page size, so to be safe it would be best to implement the pagination as documented and check the whole list, like this:
shared_folders = []
shared_folders_res = dbx.sharing_list_folders()
shared_folders += shared_folders_res.entries
while shared_folders_res.cursor:
shared_folders_res = dbx.sharing_list_folders_continue(cursor=shared_folders_res.cursor)
shared_folders += shared_folders_res.entries
if len(shared_folders) == 0:
print('User has no shared folders')
else:
print('User has at least one shared folder')
Thank you so much Greg! Exactly what I was looking for! Can I do something similar to check team folders?
Yes, team folders are considered a type of shared folder, so just building on the previous sample, you can do something like this:
team_folder_count = len([shared_folder for shared_folder in shared_folders if shared_folder.is_team_folder])
if team_folder_count == 0:
print('User has no team folders')
else:
print('User has at least one team folder')
print('User has %s team folder(s)' % team_folder_count)
Note though that this will only catch "team folders", not "team spaces". Any Dropbox Business team may be using one of those two different configurations. Check out the Team Files Guide for information on that.
Thank you Greg, I will try that.
Hi there!
If you need more help you can view your support options (expected response time for a 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!