1

Per Box example easy way to get user's root folder using below code

http://opensource.box.com/box-java-sdk/

BoxAPIConnection api = new BoxAPIConnection("your-developer-token");
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
for (BoxItem.Info itemInfo : rootFolder) {
System.out.format("[%d] %s\n", itemInfo.getID(), itemInfo.getName());
}

But if i need to access someone else info using As-user, I'm unable to use BOX SDK classes (BoxFolder, BoxFile, BoxUser...) and need to get the data only from JSON directly like below. If i do so, i'm loosing the latest features added in the new SDK. Is it the best way? How about the performance? Is there any alternative way available?

url=  new URL("https://api.box.com/2.0/folders/0");
BoxAPIRequest request = new BoxAPIRequest(api,url,"GET"); 
request.addHeader("As-User", "12345678");
BoxJSONResponse response = (BoxJSONResponse) request.send();
JsonObject responseJSON =    JsonObject.readFrom(response.getJSON());

Later get the folder properties using JsonObject / JsonArray. If i need the folder items, i need to loop the JsonArray like below

JsonArray entries = responseJSON.get("entries").asArray();

for (JsonValue entry : entries) 
               { ....}
Chuck
  • 53
  • 5

1 Answers1

3

Unfortunately, the new Java SDK beta doesn't have built-in support for "As-User" functionality yet, which makes this kind of tricky. One workaround is to use a RequestInterceptor with your BoxAPIConnection to manually add the "As-User" header to every request.

api.setRequestInterceptor(new RequestInterceptor() {
    @Override
    public BoxAPIResponse onRequest(BoxAPIRequest request) {
        request.addHeader("As-User", "user-id");

        // Returning null means the request will be sent along with our new header.
        return null;
    }
}

This should let you use the rest of the SDK normally and not have to worry about doing the API requests manually. I also created an issue for adding "As-User" support.

Greg
  • 3,731
  • 1
  • 29
  • 25