Java Requires Chilkat v11.5.0+
Chilkat2 Java
Box: Refresh and Save Rotated OAuth Tokens
See more Box Examples
Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.
See the Box API guide for setup and the complete workflow.
Chilkat Java Downloads
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;
JsonObject tokens = new JsonObject();
try {
tokens.loadFile("qa_data/tokens/box_oauth.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
OAuth2 oauth2 = new OAuth2();
oauth2.setTokenEndpoint("https://api.box.com/oauth2/token");
oauth2.setClientId("BOX_CLIENT_ID");
oauth2.setClientSecret("BOX_CLIENT_SECRET");
oauth2.setRefreshToken(tokens.stringOf("refresh_token"));
try {
oauth2.refreshAccessToken();
tokens.load(oauth2.getAccessTokenResponse());
// The previous refresh token is now invalid. Preserve this response even if
// your normal token-store write fails. Do not retry with the old refresh token.
tokens.writeFile("qa_data/tokens/box_oauth.json");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("New access and refresh tokens saved. Do not log their values.");
}
}