Android 应用程序启动时,如何保存 dropbox 用户详细信息,而不是每次都进行身份验证?

how to save dropbox user details for him not to authenticate every time when Android application starts?

我正在构建一个 android 应用程序,用户必须在其中从 Dropbox 下载图片。但是每次,用户都必须对自己进行身份验证。我希望应用程序第一次保存详细信息,之后不需要身份验证。代码如下:

protected void initialize_session(){
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);
}


protected void onResume() {
    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();;
            String accessToken = mDBApi.getSession().getOAuth2AccessToken();
        } catch (IllegalStateException e) {
            Log.i("DbAuthLog", "Error authenticating", e);
        }
    }
    super.onResume();
}

这是为了让用户返回应用。我知道解决方案必须在这两个中,但我不知道如何保存凭据。

将您的 accessToken 保存在共享 preference/SQLite 中。

例如

SharedPreferences sp = getSharedPreferences(
                                            "First_share_memory", Activity.MODE_APPEND);
                                    // save in cache memory
                                    sp.edit().putString("accesstoken", accessToken).commit();

并在 getDropboxAPI 方法中使用它:

private DropboxAPI <AndroidAuthSession> getDropboxAPI() {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

SharedPreferences  sharedpreferences = getSharedPreferences("First_share_memory", Activity.MODE_APPEND);
String savedAccessToken = sharedpreferences.getString("accesstoken", "");// get previously saved accessToken

if (!TextUtils.isEmpty(savedAccessToken)) {
    mDBApi.getSession().setOAuth2AccessToken(savedAccessToken);
}

return mDBApi;
}

有关详细信息,请参阅参考资料:

link

将您的令牌保存到 SharedPrefernce,然后相应地使用它。下面是相同的示例代码。 在 onResume 函数中进行以下更改:

protected void onResume() {
        AndroidAuthSession session = mApi.getSession();
        setLoggedIn(mApi.getSession().authenticationSuccessful());
        if (session.authenticationSuccessful()) {
            try {
                // Mandatory call to complete the auth
                session.finishAuthentication();

                // Store it locally in our app for later use
                TokenPair tokens = session.getAccessTokenPair();
                storeKeys(tokens.key, tokens.secret);
                setLoggedIn(true);
            } catch (IllegalStateException e) {
                showToast(getString(R.string.could_not_authenticate_with_dropbox)
                        + e.getLocalizedMessage());
            }
        }
        super.onResume();
    }

添加 storeKeys 和 clearKeys 函数以在 SharedPreferences 中保存值

private void storeKeys(String key, String secret) {
        // Save the access key for later
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, key);
        edit.putString(ACCESS_SECRET_NAME, secret);
        edit.commit();
    }

    private void clearKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.clear();
        edit.commit();
    }
private String[] getKeys() {
        SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        if (key != null && secret != null) {
            String[] ret = new String[2];
            ret[0] = key;
            ret[1] = secret;
            return ret;
        } else {
            return null;
        }
    }

并像下面这样初始化您的会话:

public AndroidAuthSession buildSession() {
        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session;

        String[] stored = getKeys();
        if (stored != null) {
            AccessTokenPair accessToken = new AccessTokenPair(stored[0],
                    stored[1]);
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE,
                    accessToken);
        } else {
            session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
        }
        return session;
    }

编辑:添加这三个常量,可以注释掉setLoggedIn(true)的调用;

final static private String ACCOUNT_PREFS_NAME = "prefs";
final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";