我可以使用Android SDK进行身份验证,但我遇到的一个问题(similar to this)是访问令牌在到期后永远不会刷新,即使我的刷新令牌尚未过期。这是我正在工作的场景:
提示用户身份验证。成功后,将authdata存储为json字符串。
退出应用。一小时后(访问令牌到期),启动应用程序。
我的应用会检查以前保存的令牌,在这种情况下会找到一个。然后它拉出旧的authdata并调用client.authenticate(authdata)。 注意:此时没有错误。
此时,我与客户端进行API调用,但收到错误消息称访问令牌无效。还会抛出AuthFailure异常并显示错误:“无效授权”,说明:“刷新令牌已过期”。不确定为什么会这样,因为刷新令牌不应该在同一天到期。
我已经检查了存储的令牌并与检索到的令牌进行了比较,没有任何问题。以下是一些代码片段。
Box init:
//check for previous tokens
SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
if (settings.contains(PREFS_AUTHDATA)) {
isLinked = true;
try {
String authToken = settings.getString(PREFS_AUTHDATA, "");
Log.v(LOG_TAG, "Token:\n" + authToken);
BoxAndroidOAuthData authData = new BoxJSONParser(new AndroidBoxResourceHub()).parseIntoBoxObject(authToken, BoxAndroidOAuthData.class);
Log.v(LOG_TAG, "check access: " + authData.getAccessToken() + ", refresh: " + authData.getRefreshToken());
mClient.addOAuthRefreshListener(new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
//save token
try {
String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
//save token
SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREFS_AUTHDATA, authToken);
editor.commit();
Log.v(LOG_TAG, "Tokens refreshed and updated");
} catch (BoxJSONException e) {
Log.e(LOG_TAG, "Authentication error - BoxJSONException");
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Authentication error - IOException");
e.printStackTrace();
}
}
});
mClient.authenticate(authData);
Log.v(LOG_TAG, "Authenticated with previously stored tokens");
} catch (BoxJSONException e) {
Log.e(LOG_TAG, "Authentication error - BoxJSONException");
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Authentication error - IOException");
e.printStackTrace();
}
}
开启身份验证:
Log.v(LOG_TAG, "Starting Authentication");
mCallback = (CloudSetupWebViewActivity)activity;
webview.initializeAuthFlow(activity, CLIENT_ID, CLIENT_SECRET);
mClient.authenticate(webview, true, new OAuthWebViewListener() {
@Override
public void onSslError(SslErrorHandler handler, SslError error) {
Log.e(LOG_TAG, "Authentication error - onSslError: " + error.toString());
handler.proceed();
}
@Override
public void onError(int errorCode, String description, String failingUrl) {
Log.e(LOG_TAG, "Authentication error - onError: " + description);
}
@Override
public void onAuthFlowMessage(IAuthFlowMessage message) {
}
@Override
public void onAuthFlowException(Exception e) {
Log.e(LOG_TAG, "Authentication error - onAuthFlowException: " + e.getClass().getCanonicalName());
}
@Override
public void onAuthFlowEvent(IAuthEvent event, IAuthFlowMessage message) {
if (event == OAuthEvent.OAUTH_CREATED) {
try {
OAuthDataMessage msg = (OAuthDataMessage) message;
String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(msg.getData());
Log.v(LOG_TAG, "authToken: " + authToken);
//save token
SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREFS_AUTHDATA, authToken);
editor.commit();
isLinked = true;
Log.v(LOG_TAG, "Authenticated and saved access/refresh token");
mCallback.onAuthenticationComplete();
} catch (BoxJSONException e) {
Log.e(LOG_TAG, "Authentication error - BoxJSONException");
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Authentication error - IOException");
e.printStackTrace();
}
}
}
});
mClient.addOAuthRefreshListener(new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
//save token
try {
String authToken = new BoxJSONParser(new AndroidBoxResourceHub()).convertBoxObjectToJSONString(newAuthData);
//save token
SharedPreferences settings = appContext.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREFS_AUTHDATA, authToken);
editor.commit();
Log.v(LOG_TAG, "Tokens refreshed and updated");
} catch (BoxJSONException e) {
Log.e(LOG_TAG, "Authentication error - BoxJSONException");
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, "Authentication error - IOException");
e.printStackTrace();
}
}
});
提前致谢!
答案 0 :(得分:1)
刷新oauth数据后,我认为oauth数据已刷新,但您无法使用它。
尝试使用此代码 刷新oauth数据后
String authToken = settings.getString(PREFS_AUTHDATA, "");
Log.v(LOG_TAG, "Token:\n" + authToken);
BoxAndroidOAuthData authData = newBoxJSONParser(newAndroidBoxResourceHub()).parseIntoBoxObject(authToken, BoxAndroidOAuthData.class); Log.v(LOG_TAG, "check access: " + authData.getAccessToken() + ", refresh: " + authData.getRefreshToken());