I\'ve been recently trying to implement Box in my Android app. I know how to launch the authentication activity and get BoxAndroidClient object ready to operate on it, but I
You shouldn't need to refresh the token yourself, the sdk does it for you. So even your access token is incorrect, as far as the refresh token is correct, the sdk will gets you a new access token.
The BoxAndroidOAuthData object is a parcelable so can be saved this way. It can also be serialized to a json string by toJSONString(new ObjectMapper()) and deserialized from json string by Utils.parseJSONStringIntoObject(jsonString, BoxAndroidOAuthData.class), so it can also be saved to string. Sharedpreference is one of the choices, although it may not be as secure as you want.
As a simplest(not the best) example:
1. save auth:
sharedPref.edit().putString("auth", authData.toJSONString(new ObjectMapper());
2. load auth:
BoxAndroidOAuthData authData = Utils.parseJSONStringIntoObject(sharedPref.getString("auth"), BoxAndroidOAuthData.class);
boxClient.authenticate(authData);
Please note as far as your refresh token in the BoxAndroidOAuthData is still valid, you don't need to worry about refresh the access token, the sdk refreshes it for you. In case your refresh token is invalid, sdk will throw a AuthFatalFailureException and your app need to handle it.
This is my way to handle this find below my PreferencesUtil class.
package com.omt.omtboxapi;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import com.box.boxjavalibv2.dao.BoxOAuthToken;
package com.omt.omtboxapi;
public class PreferencesUtil {
private static PreferencesUtil preferencesUtil;
private PreferencesUtil() {
}
public static PreferencesUtil getInstance() {
if (preferencesUtil == null) {
synchronized (PreferencesUtil.class) {
if (preferencesUtil == null) {
preferencesUtil = new PreferencesUtil();
}
}
}
return preferencesUtil;
}
public BoxOAuthToken getAuthToken(Context context) {
BoxOAuthToken authToken = null;
SharedPreferences preferences = context.getSharedPreferences(
"OMT_BOX_SHARED", Context.MODE_PRIVATE);
if (!preferences.getBoolean("IS_NEW", true)) {
Map<String, Object> map = new HashMap<String, Object>();
map.put(BoxOAuthToken.FIELD_ACCESS_TOKEN,
preferences.getString(BoxOAuthToken.FIELD_ACCESS_TOKEN, ""));
map.put(BoxOAuthToken.FIELD_REFRESH_TOKEN, preferences.getString(
BoxOAuthToken.FIELD_REFRESH_TOKEN, ""));
map.put(BoxOAuthToken.FIELD_TOKEN_TYPE,
preferences.getString(BoxOAuthToken.FIELD_TOKEN_TYPE, ""));
map.put(BoxOAuthToken.FIELD_EXPIRES_IN,
preferences.getInt(BoxOAuthToken.FIELD_EXPIRES_IN, 0));
authToken = new BoxOAuthToken(map);
}
return authToken;
}
public void saveAuthToken(BoxOAuthToken authToken, Context context) {
SharedPreferences preferences = context.getSharedPreferences(
"OMT_BOX_SHARED", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("IS_NEW", false);
editor.putString(BoxOAuthToken.FIELD_ACCESS_TOKEN,
authToken.getAccessToken());
editor.putString(BoxOAuthToken.FIELD_REFRESH_TOKEN,
authToken.getRefreshToken());
editor.putString(BoxOAuthToken.FIELD_TOKEN_TYPE,
authToken.getTokenType());
editor.putInt(BoxOAuthToken.FIELD_EXPIRES_IN, authToken.getExpiresIn());
editor.commit();
}
}
Now to handle Refresh things below is my approach :
client.addOAuthRefreshListener(new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
PreferencesUtil.getInstance().saveAuthToken(
(BoxOAuthToken) newAuthData, MainActivity.this);
}
});
NOTE :
The Keys that I am using in Preference is available in BoxOAuthToken so DO NOT CHANGE IT OTHERWISE YOUR CODE WILL NOT WORK..
THOSE KEYS ARE :
public static final String FIELD_ACCESS_TOKEN = "access_token";
public static final String FIELD_EXPIRES_IN = "expires_in";
public static final String FIELD_TOKEN_TYPE = "token_type";
public static final String FIELD_REFRESH_TOKEN = "refresh_token";