Java Requires Chilkat v11.5.0+
Chilkat2 Java
Box: Get a Service Account Token with JWT
See more Box Examples
Use the encrypted private key in a Box application configuration file to sign a short-lived assertion and exchange it for an access token. Configure and authorize a JWT Server app first. The enterprise subject obtains the service-account token.
See the Box API guide for setup and the complete workflow.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.HttpCurl;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Jwt;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.Prng;
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;
JsonObject config = new JsonObject();
try {
config.loadFile("qa_data/tokens/box_jwt_config.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
PrivateKey key = new PrivateKey();
try {
key.loadEncryptedPem(config.stringOf("boxAppSettings.appAuth.privateKey"), config.stringOf("boxAppSettings.appAuth.passphrase"));
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject header = new JsonObject();
header.updateString("alg", "RS256");
header.updateString("typ", "JWT");
header.updateString("kid", config.stringOf("boxAppSettings.appAuth.publicKeyID"));
Jwt jwt = new Jwt();
Prng prng = new Prng();
String nonce;
try {
nonce = prng.genRandom(32, "hex");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
JsonObject claims = new JsonObject();
claims.updateString("iss", config.stringOf("boxAppSettings.clientID"));
claims.updateString("sub", config.stringOf("enterpriseID"));
claims.updateString("box_sub_type", "enterprise");
claims.updateString("aud", "https://api.box.com/oauth2/token");
claims.updateString("jti", nonce);
claims.updateInt("exp", jwt.genNumericDate(45));
String assertion;
try {
assertion = jwt.createJwtPk(header.emit(), claims.emit(), key);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
HttpCurl curl = new HttpCurl();
curl.setVar("client_id", config.stringOf("boxAppSettings.clientID"));
curl.setVar("client_secret", config.stringOf("boxAppSettings.clientSecret"));
curl.setVar("assertion", assertion);
curl.clearTargetOutput("*");
curl.clearVar("access_token");
curl.addTargetOutput("access_token", "access_token");
try {
curl.doYourThing("curl -X POST \"https://api.box.com/oauth2/token\" --data-urlencode \"grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer\" --data-urlencode \"client_id={{client_id}}\" --data-urlencode \"client_secret={{client_secret}}\" --data-urlencode \"assertion={{assertion}}\"");
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());
return;
}
if (!curl.varDefined("!")) {
System.out.println("Expected response fields are missing.");
return;
}
// Reuse the token in this HttpCurl instance. JWT tokens are renewed by making
// a new assertion/token request, not by using a refresh token.
System.out.println("JWT access token obtained.");
}
}