Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

amedeomantica's avatar
amedeomantica
Explorer | Level 4
8 years ago
Solved

How to access Team Folders with Java API ?

Hi, I'm using dropbox V2 API with the Java library: dropbox-sdk-java

While I can access all team member's folders I cannot figure out how to access the global Team Folders.

Any hint ?

Thank you

Amedeo

  • chirstius's avatar
    chirstius
    8 years ago

    Thank you for posting those samples amedeomantica

     

    Because your Dropbox Team uses team space and member folders I'd like to make a suggestion. First, make sure you have the latest Java SDK (3.0.7).

     

    Then add a call to get the current account information and root namespace:

     

    FullAccount account = adminClient.users().getCurrentAccount();
    String rootNS = account.getRootInfo().getRootNamespaceId();

     

     

    Next, replace your call to listFolder() with this:

     

     

    final ListFolderResult listFolderResult = dropBoxConnection.memberClient().withPathRoot(PathRoot.namespaceId(rootNS)).files().listFolder(path);

     

    If there are issues with that, you can try varying the composition of your path string - try just using the literal string pathname vs the "ns:" format. Remember that all pathing will be relative to the root namespace (the Team root folder).

     

    What the withPathRoot() method does is to "root" the chained API call at the given namespace. In your case, while you may have the proper namespace ID for the Team Folder, if you are not accessing it via the root namespace ID, it will not be visible to your user. This is because by default all calls without an explicit path root set are rooted in the home namespace (your user folder), not the team root where Team Folders live.

     

    I suggest looking over "User's Home Folder & Team Root Folder" and the "Using a Namespace to Identify Content" in the Namespace Guide:

    https://www.dropbox.com/developers/reference/namespace-guide

     

    Hopefully, this gets you past the error.

     

    -Chuck

     

     

9 Replies

  • Hello amedeomantica,

     

    Can you clarify if this is a team-linked app, or a user-based app being linked to a team-based account?

     

    In either case, I would suggest our namespace reference guide as a resource that may help with resolving this:

    https://www.dropbox.com/developers/reference/namespace-guide

     

    Of specific interest (if you are using a team-linked app) will be the namespacesList() and namespacesListContinue() methods to retrieve Team Folder namespace IDs:

    DbxTeamClientV2 client = new DbxTeamClientV2(config, ACCESS_TOKEN);
    TeamNamespacesListResult namespaces = client.team().namespacesList(); // with an optional limit param

     

    Within the results of that call, you can examine the NamespaceType value to determine what kind of folder it is:

    for ( NamespaceMetadata namespace : namespaces.getNamespaces()) {
        if ( namespace.getNamespaceType() == NamespaceType.TEAM_FOLDER ) {
    System.out.println("Team Folder: " + namespace.getName());
    } }

    And once you have that you can use the namespace ID ( getNamespaceId() ) in other calls to traverse the Team Folder. Be aware you may need to prefix the namespace ID with "ns:" for it to be an acceptable path parameter to methods like listFolder() and you will need to impersonate (asMember()) users with appropriate access to any given folder to list its contents.

     

    Hope that helps,

     

    -Chuck

     

  • amedeomantica's avatar
    amedeomantica
    Explorer | Level 4
    8 years ago

    Hi @chirstius,

     

    Mine is a team linked app with "Team member file access"

     

    I followed your suggestion in order to get the namespaces list and find the team folder.

    Hovewer if I try to list team folder files I go into error

     

    Caused by: com.dropbox.core.v2.files.ListFolderErrorException: Exception in 2/files/list_folder: {".tag":"path","path":"not_found"}
    	at com.dropbox.core.v2.files.DbxUserFilesRequests.listFolder(DbxUserFilesRequests.java:1425)
    	at com.dropbox.core.v2.files.DbxUserFilesRequests.listFolder(DbxUserFilesRequests.java:1466)

    If I try any other namespace ( member folders, shared folders ) everything works.

    Any idea ?

     

  • chirstius's avatar
    chirstius
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago

    Are you certain the member ID you are using to access the team folder actually has access to it? You may need to switch to a different user via asMember(). Can you validate that first? Then we can dig a little deeper...

     

    Thanks!

     

    -Chuck

  • amedeomantica's avatar
    amedeomantica
    Explorer | Level 4
    8 years ago
    Yeah, the member is correct and have access to the folder (outside the API)
  • chirstius's avatar
    chirstius
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago

    amedeomantica,

     

    Are you able to access other Team Folders as that user? Is it just a single specific folder that is failing or all Team Folders?

     

    Also, if you wouldn't mind, can you post a sample of the code you are using to list the Team Folder and a recent error response?

     

    Thanks,

     

    -Chuck

  • amedeomantica's avatar
    amedeomantica
    Explorer | Level 4
    8 years ago

    Hi chirstius,

    Here the relevant code:

     

     

    private DbxTeamClientV2 client;
    
    DbxRequestConfig config = DbxRequestConfig.newBuilder("something").build();
    
    client = new DbxTeamClientV2(config, accessToken);
    
    public DbxClientV2 memberClient() {
        return client.asMember("dbmid:userIdWithFullAccess");
    }
    
    public NamespaceMetadata teamFolderNameSpace() throws DbxException {
    
        TeamNamespacesListResult namespaces = client.team().namespacesList();
    
        for ( NamespaceMetadata namespace : namespaces.getNamespaces()) {
    //this correctly list all the available namespaces, including the team folder log.info(namespace.getName() + "/" + namespace.getNamespaceId)); }
    //correctly returns the teamfolder for ( NamespaceMetadata namespace : namespaces.getNamespaces()) { if ( namespace.getNamespaceType() == NamespaceType.TEAM_FOLDER ) { return namespace; } } return null; }

     

    then:

     

    public Result test() throws Exception {

    final NamespaceMetadata namespaceMetadata = dropBoxConnection.teamFolderNameSpace();
    if(namespaceMetadata != null) {
    String namespaceId = namespaceMetadata.getNamespaceId();

    // MEDIA folder exists and I access it from web/desktop (I'm the same user as memberClient)
    // But is not working from here
    // also tried without "MEDIA" (listing team folder root), no luck
    String path = "ns:" + namespaceId + "/MEDIA";

    //This works, putting any namespace ID (I can access), except main team folder
    //String path = "ns:ANY_OTHER_NAMESPACE_ID_I_CAN_ACCESS";

    log.info(path);

    final ListFolderResult listFolderResult = dropBoxConnection.memberClient().files().listFolder(path);
    listFolderResult.getEntries().forEach(r -> {
    log.info(r.getName());
    });
    }

    return ok("ok");
    }

    error:

     

    Caused by: com.dropbox.core.v2.files.ListFolderErrorException: Exception in 2/files/list_folder: {".tag":"path","path":"not_found"}
    	at com.dropbox.core.v2.files.DbxUserFilesRequests.listFolder(DbxUserFilesRequests.java:1425)
    	at com.dropbox.core.v2.files.DbxUserFilesRequests.listFolder(DbxUserFilesRequests.java:1466)
    	at controllers.HomeController.test(HomeController.java:133)
    

     

  • chirstius's avatar
    chirstius
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago

    Thank you for posting those samples amedeomantica

     

    Because your Dropbox Team uses team space and member folders I'd like to make a suggestion. First, make sure you have the latest Java SDK (3.0.7).

     

    Then add a call to get the current account information and root namespace:

     

    FullAccount account = adminClient.users().getCurrentAccount();
    String rootNS = account.getRootInfo().getRootNamespaceId();

     

     

    Next, replace your call to listFolder() with this:

     

     

    final ListFolderResult listFolderResult = dropBoxConnection.memberClient().withPathRoot(PathRoot.namespaceId(rootNS)).files().listFolder(path);

     

    If there are issues with that, you can try varying the composition of your path string - try just using the literal string pathname vs the "ns:" format. Remember that all pathing will be relative to the root namespace (the Team root folder).

     

    What the withPathRoot() method does is to "root" the chained API call at the given namespace. In your case, while you may have the proper namespace ID for the Team Folder, if you are not accessing it via the root namespace ID, it will not be visible to your user. This is because by default all calls without an explicit path root set are rooted in the home namespace (your user folder), not the team root where Team Folders live.

     

    I suggest looking over "User's Home Folder & Team Root Folder" and the "Using a Namespace to Identify Content" in the Namespace Guide:

    https://www.dropbox.com/developers/reference/namespace-guide

     

    Hopefully, this gets you past the error.

     

    -Chuck

     

     

  • chirstius's avatar
    chirstius
    Icon for Dropbox Staff rankDropbox Staff
    8 years ago

    Glad to hear it, don't hesitate to reach out again if there is anything else you need.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

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, Facebook or Instagram.

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!