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: 

Check if user has access to any shared folders

Check if user has access to any shared folders

JuliaNowicka
Explorer | Level 4
Go to solution

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

 

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

[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')

 

View solution in original post

4 Replies 4

Greg-DB
Dropbox Staff
Go to solution

[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')

 

JuliaNowicka
Explorer | Level 4
Go to solution

Thank you so much Greg! Exactly what I was looking for! Can I do something similar to check team folders? 

Greg-DB
Dropbox Staff
Go to solution

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.

 

JuliaNowicka
Explorer | Level 4
Go to solution

Thank you Greg, I will try that. 

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    JuliaNowicka Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?