在Box Android API中加载,保存和使用身份validation数据

我最近一直试图在我的Android应用程序中实现Box。 我知道如何启动身份validation活动并使BoxAndroidClient对象准备好对其进行操作,但我不知道如何保存令牌(SharedPreferences?),加载它们然后使用加载的令牌进行身份validation,因此用户不会每次他想要在云端访问他的文件时登录他的盒子帐户。

我试图刷新以前保存的令牌(因为Exception告诉我我的AccessToken不正确)。

BoxAndroidOAuthData data = new BoxAndroidOAuthData(new HashMap(){ 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))); 

我还有另外一个例外:

 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) 

由于我没有找到任何关于如何在Java中正确执行auth部分的教程(SDK中包含的示例不包括任何保存令牌的方法),有没有人可以为此提供一个很好的示例?

你不应该自己刷新令牌,sdk会为你做。 因此,即使您的访问令牌不正确,只要刷新令牌正确,sdk将为您提供新的访问令牌。

BoxAndroidOAuthData对象是一个parcelable,因此可以这种方式保存。 它也可以通过toJSONString(new ObjectMapper())序列化为json字符串,并通过Utils.parseJSONStringIntoObject(jsonString,BoxAndroidOAuthData.class)从json字符串反序列化,因此它也可以保存为字符串。 共享偏好是其中一种选择,尽管它可能不如您想要的那么安全。

作为一个最简单的(不是最好的)示例:1。保存auth: sharedPref.edit().putString("auth", authData.toJSONString(new ObjectMapper()); 2. load auth: BoxAndroidOAuthData authData = Utils.parseJSONStringIntoObject(sharedPref.getString("auth"), BoxAndroidOAuthData.class); boxClient.authenticate(authData);请注意,只要BoxAndroidOAuthData中的刷新令牌仍然有效,您无需担心刷新访问令牌,即sdk为你刷新它。如果你的刷新令牌无效,sdk将抛出一个AuthFatalFailureException,你的应用程序需要处理它。

这是我在PreferencesUtil类下面处理这个查找的方法。

 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 map = new HashMap(); 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(); } 

}

现在处理下面的刷新事情是我的方法:

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

注意 :

我在Preference中使用的键在BoxOAuthToken中可用,所以不要改变它,否则你的代码将无效。

这些钥匙是:

 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";