我正在使用 Android Dropbox API。在我的应用程序的主要 Activity 中,我正在对 dropbox api 进行身份验证调用。问题是,每次我的应用程序启动时,用户都必须点击“允许”才能授予应用程序访问其保管箱的权限。我的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//clearKeys();
//Log.e(TAG, "keys cleared");
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startAuthentication(Main.this);
Log.e(TAG, "started authentication");
protected void onResume() {
super.onResume();
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// MANDATORY call to complete auth.
// Sets the access token on the session
mDBApi.getSession().finishAuthentication();
if(mDBApi.getSession().authenticationSuccessful()){
Log.e(TAG, "Authentication finished");
}
AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();
// Provide your own storeKeys to persist the access token pair
// A typical way to store tokens is using SharedPreferences
storeKeys(tokens.key, tokens.secret);
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
}//end of onResume()
我需要找到一种方法来知道该应用程序是否经过身份验证,以便在这种情况下我可以绕过身份验证。我现在不知道该怎么做。有人可以帮忙吗?
最佳答案
在进行身份验证之前,请检查存储的访问 token 是否仍然有效(当然,如果您有的话)。为此,只需向通过 OAuth 公开的任何方法发出授权请求即可。最好调用一个能产生简短输出的方法。 如果此调用因 token 无效/过期而失败,请继续进行身份验证。此时更新存储的 token ,其余部分应该像以前一样工作
关于java - 如何检查 Dropbox 用户 session 是否有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14926620/