Download, save, and use authentication data in the Android Box API

I recently tried to implement Box in my Android app. I know how to start authentication and get a BoxAndroidClient object ready to work on it, but I have no idea how to save tokens (SharedPreferences?), Load them and then authenticate using downloaded tokens, so the user will not have it every time. when he would like to access his files in the cloud.

I tried updating the previously saved tokens (due to an exception that informed me that my AccessToken is incorrect).

BoxAndroidOAuthData data = new BoxAndroidOAuthData(new HashMap<String, Object>(){ private static final long serialVersionUID = 1L; { put(BoxAndroidOAuthData.FIELD_ACCESS_TOKEN, prefs.acc); put(BoxAndroidOAuthData.FIELD_REFRESH_TOKEN, prefs.ref); put(BoxAndroidOAuthData.FIELD_EXPIRES_IN, prefs.exp); put(BoxAndroidOAuthData.FIELD_TOKEN_TYPE, prefs.typ); } }); data = new BoxAndroidOAuthData(client.getOAuthManager().refreshOAuth(BoxOAuthRequestObject.refreshOAuthRequestObject(data.getRefreshToken(), C, S))); 

And I have one more exception:

 07-02 22:07:16.433: W/System.err(4684): com.box.restclientv2.exceptions.BoxRestException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id (for class com.box.boxjavalibv2.dao.BoxServerError) 07-02 22:07:16.433: W/System.err(4684): at [Source: java.io.StringReader@b55b2c78 ; line: 1, column: 69] 07-02 22:07:16.433: W/System.err(4684): at com.box.restclientv2.responseparsers.DefaultBoxJSONResponseParser.parse(DefaultBoxJSONResponseParser.java:75) 07-02 22:07:16.433: W/System.err(4684): at com.box.boxjavalibv2.responseparsers.ErrorResponseParser.parse(ErrorResponseParser.java:31) 07-02 22:07:16.433: W/System.err(4684): at com.box.restclientv2.responses.DefaultBoxResponse.parseResponse(DefaultBoxResponse.java:51) 07-02 22:07:16.433: W/System.err(4684): at com.box.boxjavalibv2.resourcemanagers.BoxResourceManager.getResponseAndParse(BoxResourceManager.java:168) 07-02 22:07:16.433: W/System.err(4684): at com.box.boxjavalibv2.resourcemanagers.BoxResourceManager.getResponseAndParseAndTryCast(BoxResourceManager.java:143) 07-02 22:07:16.433: W/System.err(4684): at com.box.boxjavalibv2.resourcemanagers.BoxOAuthManager.refreshOAuth(BoxOAuthManager.java:68) 


Since I have not found any tutorials on how to correctly execute the auth part in Java (the samples included in the SDK do not cover any way of storing tokens), can anyone serve as a good example for this?

+4
source share
2 answers

You do not need to update the token yourself, sdk does it for you. Thus, even your access token is incorrect, since the update token is correct, sdk will receive a new access token.

The BoxAndroidOAuthData object is simple, so you can save it this way. It can also be serialized to a json string using the JSONString function (new ObjectMapper ()) and deserialized from a json string using Utils.parseJSONStringIntoObject (jsonString, BoxAndroidOAuthData.class), so it can also be saved to a string. Sharedpreference is one option, although it may not be as secure as you want.

As a simple (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); Note how valid your BoxAndroidOAuthData update token is still, you don’t have to worry about updating the access token, sdk updates it for you. If it’s for you. your update token is invalid, sdk will throw an AuthFatalFailureException, and your application should handle it.

+2
source

This is my way of dealing with 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 handle Update below my approach below:

  client.addOAuthRefreshListener(new OAuthRefreshListener() { @Override public void onRefresh(IAuthData newAuthData) { PreferencesUtil.getInstance().saveAuthToken( (BoxOAuthToken) newAuthData, MainActivity.this); } }); 

NOTE:

The keys that I use in Preferences are available in the BoxOAuthToken, so DO NOT CHANGE THIS SOMETIMES YOUR CODE DOES NOT WORK.

THESE KEYS:

 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"; 
+2
source

Source: https://habr.com/ru/post/1489369/


All Articles