Level up your Box knowledge with brand new learning paths on Box University. Visit training.box.com to get started

How to fetch BOX application folders and file metadata information using Box Java SDK?

New post

Comments

5 comments

  • kendomen

    here's a code snippet...

     

            ...
    BoxAPIConnection api = getBoxAPIConnectionFromState(client_id, client_secret, token, refresh_token, stateConfPath); saveBoxAPIConnect(api, stateConfPath); listFolder(BoxFolder.getRootFolder(api), 1); } private static void listFolder(BoxFolder folder, int depth) { for (BoxItem.Info itemInfo : folder) { String indent = ""; for (int i = 0; i < depth; i++) { indent += " "; } System.out.println(indent + itemInfo.getName() + " [" + itemInfo.getID()+ "]"); if (itemInfo instanceof BoxFile.Info) { BoxFile file = (BoxFile)itemInfo.getResource(); try { Metadata metadata = file.getMetadata(); System.out.println(indent + "metadata: " + metadata.toString()); } catch (BoxAPIException ae) { // 404 - you can't do anything about for now } } else if (itemInfo instanceof BoxFolder.Info) { BoxFolder childFolder = (BoxFolder) itemInfo.getResource(); if (depth < 100) { listFolder(childFolder, depth + 1); } } } }

    and sample output:

     

    Box Reports [removed for privacy7]
            shared_links_run_on_6-8-16__8-**removed**-AM.xlsx [removed for privacy33]
            metadata: {"foo":"bar","$type":"properties","$parent":"file_69084985633","$id":"7b93fbcb-63bb-45**removed**f5a-07b96c2d171f","$version":0,"$typeVersion":2,"$template":"properties","$scope":"global"}

    0
    Comment actions Permalink
  • ArpitBora

    Thanks for reply
    I tried your given code using Developer Token to create an API connection :

    BoxAPIConnection api = new BoxAPIConnection(DEVELOPER_TOKEN);

     

     but I'm getting following BoxAPIException :

     

    com.box.sdk.BoxAPIException: The API returned an error code: 404
    	at com.box.sdk.BoxAPIResponse.(BoxAPIResponse.java:70)
    	at com.box.sdk.BoxJSONResponse.(BoxJSONResponse.java:30)
    	at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:423)
    	at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:209)
    	at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:184)
    	at com.box.sdk.BoxFile.getMetadata(BoxFile.java:623)
    	at com.box.sdk.BoxFile.getMetadata(BoxFile.java:611)

     

    Getting this exception at the time of metadata creation :

    Metadata metadata = file.getMetadata();

     

     

    0
    Comment actions Permalink
  • kdomen

    I think that's why I have it wrapped in a try/catch since "as-user" calls don't work in box-java-sdk.

     

     

    try {
         Metadata metadata = file.getMetadata();
         System.out.println(indent + "metadata: " + metadata.toString());
    }
         catch (BoxAPIException ae) {
         // 404 - you can't do anything about for now
    }

     

    0
    Comment actions Permalink
  • ArpitBora

    "as-user calls don't work in box-java-sdk" I didn't get it can you please elaborate it.

    If it does not work in java then there is any another way to fetch BOX folder and file metadata using box-java-sdk?

    0
    Comment actions Permalink
  • kendomen

    You'll be able to get your AppUser's content but you won't be able to get another user (managed user's) content.

    0
    Comment actions Permalink

Please sign in to leave a comment.