Perl Requires Chilkat v11.5.0+
Perl
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 Perl Downloads
use chilkat();
$success = 0;
# Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
# See Global Unlock Sample.
# Guide: Box API Guide.
$success = 0;
$tokens = chilkat::CkJsonObject->new();
$success = $tokens->LoadFile("qa_data/tokens/box_oauth.json");
if ($success != 1) {
print $tokens->lastErrorText() . "\r\n";
exit;
}
$oauth2 = chilkat::CkOAuth2->new();
$oauth2->put_TokenEndpoint("https://api.box.com/oauth2/token");
$oauth2->put_ClientId("BOX_CLIENT_ID");
$oauth2->put_ClientSecret("BOX_CLIENT_SECRET");
$oauth2->put_RefreshToken($tokens->stringOf("refresh_token"));
$success = $oauth2->RefreshAccessToken();
if ($success != 1) {
print $oauth2->lastErrorText() . "\r\n";
exit;
}
$success = $tokens->Load($oauth2->accessTokenResponse());
if ($success != 1) {
print $tokens->lastErrorText() . "\r\n";
exit;
}
# 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.
$success = $tokens->WriteFile("qa_data/tokens/box_oauth.json");
if ($success != 1) {
print $tokens->lastErrorText() . "\r\n";
exit;
}
print "New access and refresh tokens saved. Do not log their values." . "\r\n";