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

jraymonds's avatar
jraymonds
Explorer | Level 4
2 years ago

Trouble enumerating directories in a team space with SwiftyDropbox on macOS

I know this is a newbie issue but I cannot seem to find the right track to get over this learning curve...

 

I can successfully authenticate on macOS to this point:

 

let client = DropboxClientsManager.authorizedTeamClient

 

Going off examples it seems to begin an enumeration I would then want to do something like this:

 

client!.asMember("me").files.listFolder(path: "")

 

The problem I am having is where do I get the member ID for "me"? To keep things simple I would like to use the account that authorized the app. I did see this in the Dropbox community:

 

"If you mean the particular member that happened to authorize the app to connect to the team, you can look them up using /2/team/token/get_authenticated_admin. Note that this will vary by access token though, and isn't necessarily the current end-user of your app. It also won't necessarily be available for every access token."

 

Yet, using SwiftyDropbox that doesn't seem particularly helpful. (In other words, I am not sure how to get that out of the Swift SDK) The note above indicates that it might not be available for every access token. Since I have no idea when it would break, would looking it up from a known email be the better approach? Of course all of this assumes I am going down the right track to begin with.

 

There are two side questions to this as well. First, since this is an in-house app in a controlled space do I need to worry about member IDs changing? Or, would it be "safe" to just hard code the ID when I know what it actually is? Second, I am looking to enumerate the team space the account has access to and not their personal space. I believe I also saw references to differentiate between these roots once I have access. How should that part be handled?

  • jraymonds First, you should decide if you need your app to be linked to the team or linked to the user. When you use any "team scopes", the app would be linked to the entire team itself. If you don't use any team scopes, the app would be linked to the user account only.

     

    If you only need access to user functionality, such as things under FilesRoutes like listFolder/listFolderContinue, it would be best to not enable any team scopes and only use user scopes. That way, the app gets linked to the account and doesn't have unnecessary access to team operations. It would also mean that you don't need to specify the member ID, as its only connected to one account anyway.

     

    Also, whether the app is team-linked to user-linked, you would use DropboxClient.withPathRoot to set the Dropbox-API-Path-Root header if you need to access the team space. Accessing the team space that an connected account has access to doesn't require team scopes. Check out the Team Files Guide for more context on accessing the team space.

     

    You can call getCurrentAccount on UsersRoutes using a DropboxClient.

     

    So, for a user-linked app, you could do something like this:

     

    // DropboxClientsManager.authorizedClient is a DropboxClient
    DropboxClientsManager.authorizedClient!.users.getCurrentAccount().response { response, error in
        if let account = response {
            let rootNamespaceId = account.rootInfo.rootNamespaceId  // this gets the root namespace ID for the account, which is the team space if the account has a team space
            DropboxClientsManager.authorizedClient = DropboxClientsManager.authorizedClient!.withPathRoot(Common.PathRoot.root(rootNamespaceId))  // this sets the root for the client to the root found above, e.g., the team space
            DropboxClientsManager.authorizedClient!.files.listFolder(path: "").response { response, error in
                if let listing = response {
                    print(listing)
                } else {
                    print(error!)
                }
            } // and so on, handling the result and calling listFolderContinue as needed
        } else {
            print(error!)
        }
    }

     

     

    Alternatively, if you do need the app to be team-linked for whatever reason, you would get a DropboxClient from asMember on DropboxTeamClient. You can get a member ID from other methods like tokenGetAuthenticatedAdmin, membersGetInfoV2, membersListV2/membersListContinueV2, etc. on TeamRoutes.

     

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    jraymonds First, you should decide if you need your app to be linked to the team or linked to the user. When you use any "team scopes", the app would be linked to the entire team itself. If you don't use any team scopes, the app would be linked to the user account only.

     

    If you only need access to user functionality, such as things under FilesRoutes like listFolder/listFolderContinue, it would be best to not enable any team scopes and only use user scopes. That way, the app gets linked to the account and doesn't have unnecessary access to team operations. It would also mean that you don't need to specify the member ID, as its only connected to one account anyway.

     

    Also, whether the app is team-linked to user-linked, you would use DropboxClient.withPathRoot to set the Dropbox-API-Path-Root header if you need to access the team space. Accessing the team space that an connected account has access to doesn't require team scopes. Check out the Team Files Guide for more context on accessing the team space.

     

    You can call getCurrentAccount on UsersRoutes using a DropboxClient.

     

    So, for a user-linked app, you could do something like this:

     

    // DropboxClientsManager.authorizedClient is a DropboxClient
    DropboxClientsManager.authorizedClient!.users.getCurrentAccount().response { response, error in
        if let account = response {
            let rootNamespaceId = account.rootInfo.rootNamespaceId  // this gets the root namespace ID for the account, which is the team space if the account has a team space
            DropboxClientsManager.authorizedClient = DropboxClientsManager.authorizedClient!.withPathRoot(Common.PathRoot.root(rootNamespaceId))  // this sets the root for the client to the root found above, e.g., the team space
            DropboxClientsManager.authorizedClient!.files.listFolder(path: "").response { response, error in
                if let listing = response {
                    print(listing)
                } else {
                    print(error!)
                }
            } // and so on, handling the result and calling listFolderContinue as needed
        } else {
            print(error!)
        }
    }

     

     

    Alternatively, if you do need the app to be team-linked for whatever reason, you would get a DropboxClient from asMember on DropboxTeamClient. You can get a member ID from other methods like tokenGetAuthenticatedAdmin, membersGetInfoV2, membersListV2/membersListContinueV2, etc. on TeamRoutes.

     

    • jraymonds's avatar
      jraymonds
      Explorer | Level 4

      Okay, so this is huge for me. I still need to clean up the code on my end but I seem to have access now AND to the root team space which is exactly what I need. I was adding unneeded complexity assuming I needed to open up access to a business account with the team calls such as:

      DropboxClientsManager.setupWithTeamAppKeyDesktop(kDropboxAppKey)
      
      DropboxClientsManager.handleRedirectURLTeam(url, completion: oauthCompletion)
      
      DropboxClientsManager.authorizedTeamClient

      However, with your wisdom above, I don't need the "Team" calls even though I am accessing a Team space. As dumb as this sounds it was the stumbling block for me getting things to work! 

  • Здравко's avatar
    Здравко
    Legendary | Level 20

    Hi jraymonds,

    What about using getCurrentAccount() (or /2/users/get_current_account)? 🧐 The info there - teamMemberId - may be more useful about the "me". 😉

     


    jraymonds wrote:

    ... First, since this is an in-house app in a controlled space do I need to worry about member IDs changing? ...


    Every user has its own member id that doesn't change. So, if you mean is this id changes - clearly No. If you mean is it stay the same for any member in your account - definitely No.

     


    jraymonds wrote:

    ... Or, would it be "safe" to just hard code the ID when I know what it actually is? ...


    Are you the only member for your account would use that application? 🤔 Yes -> Yes. No -> No.... Generally it's not a good idea. Let's say if at some moment you decide to change your member representation, for instance - What's going on? 👈🙋 You have to edit your application every time. 😁

     


    jraymonds wrote:

    ... Second, I am looking to enumerate the team space the account has access to and not their personal space. I believe I also saw references to differentiate between these roots once I have access. How should that part be handled?


    There is info too about the account's root namespace and member's home namespace; just ignore (jump over) your home namespace while enumerating. 😉 That's it.

    Hope this helps.

    • jraymonds's avatar
      jraymonds
      Explorer | Level 4

      I actually tried to figure out:

       

      getCurrentAccount()

       

      However, I guess my issue is I do not know where to access it from. It is not part of the DropboxClientsManager, Team, or even the DropboxClientsManager.authorizedTeamClient classes. I know I am missing something dumb on this but I cannot seem to figure it out.

       

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

5,909 PostsLatest Activity: 2 hours ago
333 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!