Android™ Requires Chilkat v11.5.0+
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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;
CkJsonObject tokens = new CkJsonObject();
success = tokens.LoadFile("qa_data/tokens/box_oauth.json");
if (success != true) {
Log.i(TAG, tokens.lastErrorText());
return;
}
CkOAuth2 oauth2 = new CkOAuth2();
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 != true) {
Log.i(TAG, oauth2.lastErrorText());
return;
}
success = tokens.Load(oauth2.accessTokenResponse());
if (success != true) {
Log.i(TAG, tokens.lastErrorText());
return;
}
// 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 != true) {
Log.i(TAG, tokens.lastErrorText());
return;
}
Log.i(TAG, "New access and refresh tokens saved. Do not log their values.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}