How to fetch BOX application folders and file metadata information using Box Java SDK?
Hi all,
I'm new to BOX, I want to fetch metadata information of my BOX application folders and files using Java SDK. After getting metadata information I want to save it in JSON format.
Can you please guide me to achieve this.
Thank you,
Arpit
-
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"} -
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();
-
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 }
サインインしてコメントを残してください。
コメント
5件のコメント