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

Box: Authorize a User with OAuth 2.0

See more Box Examples

Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.

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.JsonObject;
import com.chilkatsoft.chilkat2.OAuth2;

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;

        OAuth2 oauth2 = new OAuth2();
        oauth2.setListenPort(3017);
        oauth2.setAuthorizationEndpoint("https://account.box.com/api/oauth2/authorize");
        oauth2.setTokenEndpoint("https://api.box.com/oauth2/token");
        oauth2.setClientId("BOX_CLIENT_ID");
        oauth2.setClientSecret("BOX_CLIENT_SECRET");
        // Use Box's documented confidential-client authorization-code flow.
        oauth2.setCodeChallenge(false);
        String url;
        try {
            url = oauth2.startAuth();

            oauth2.launchBrowser(url);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int elapsedMs = 0;
        while ((elapsedMs < 120000) && (oauth2.getAuthFlowState() < 3)) {
            oauth2.sleepMs(100);
            elapsedMs = elapsedMs + 100;
        }

        if (oauth2.getAuthFlowState() < 3) {
            oauth2.cancel();
            System.out.println("Authorization timed out.");
            return;
        }

        if (oauth2.getAuthFlowState() != 3) {
            System.out.println("Authorization was denied or failed. State: " + oauth2.getAuthFlowState());
            return;
        }

        // Retain both tokens in your application's protected token store.
        // The variables below are deliberately not printed.
        String accessToken = oauth2.getAccessToken();
        String refreshToken = oauth2.getRefreshToken();
        // For this local-development example only, save in a directory restricted to this user.
        // Create qa_data/tokens before running; never put this file under a web root.
        JsonObject tokens = new JsonObject();
        try {
            tokens.load(oauth2.getAccessTokenResponse());

            tokens.writeFile("qa_data/tokens/box_oauth.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Authorization complete; token response saved in the protected local token file.");
    }
}