Sample code for 30+ languages & platforms
Java Requires Chilkat v11.5.0+

Box: Identify the User Behind an Access Token

See more Box Examples

Identify the account whose permissions and root folder the token uses. Start here when an app cannot see files visible in a personal Box account.

See the Box API guide for setup and the complete workflow.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.HttpCurl;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
        // See Global Unlock Sample.
        // Guide: Box API Guide.

        success = false;

        // Supply an unexpired token obtained with one of the Box authentication examples.
        // A token represents a particular user or service account. IDs must be accessible to it.
        HttpCurl curl = new HttpCurl();
        curl.setVar("access_token", "BOX_ACCESS_TOKEN");

        curl.clearTargetOutput("*");
        StringBuilder authorizedCommand1 = new StringBuilder();
        authorizedCommand1.setString("curl -X GET \"https://api.box.com/2.0/users/me?fields=id,name,login\" -H \"Authorization: Bearer {{access_token}}\"");
        int headerReplacement1 = authorizedCommand1.replace("{{access_token}}", curl.getVar("access_token"));
        try {
            curl.doYourThing(authorizedCommand1.getAsString());
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((!success) || (curl.getStatusCode() != 200)) {
            System.out.println("Box request failed. HTTP status: " + curl.getStatusCode());
            System.out.println("Failure reason: " + curl.getFailReason());
            System.out.println(curl.getResponseBodyStr());
            return;
        }

        System.out.println(curl.getResponseBodyStr());
    }
}