Android- Dropbox api始终要求链接帐户

时间:2015-02-09 03:25:22

标签: android api mobile dropbox

我正在集成Dropbox核心API,因此我的应用可以存储文件和检索文件。问题是每次我访问Dropbox时,它都会请求我再次链接帐户。我发布了我用来登录和存储我的Dropbox令牌的代码。

public static DropBoxManager getInstance(Context context){
        if(instance == null){
            instance = new DropBoxManager();
            this.context = context;

            prefs = context.getSharedPreferences(DB_PREFS, Context.MODE_PRIVATE);
            AndroidAuthSession session = loadDBSession();
            mDBApi = new DropboxAPI<AndroidAuthSession>(session);


        }
        return instance;
    }

private static AndroidAuthSession loadDBSession(){
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    return loadAuthToken(session);
}

/**
 * This method starts the auth process for Dropbox. 
 * The API will launch a webview if needed for the auth to our app.
 * Listen in oResume to call auth successful.  
 * if needed. 
 * @param activty
 */
public static void startOAuth2(Activity activity){
    if (!mDBApi.getSession().isLinked()) {
        mDBApi.getSession().startOAuth2Authentication(activity);
    }else{
        executeDropboxProcess();
    }
}

public static boolean isAuthenticationSuccessful(){
    if (mDBApi.getSession().authenticationSuccessful()) {
        try {
            // Required to complete auth, sets the access token on the session
            mDBApi.getSession().finishAuthentication();
            storeAuthToken(mDBApi.getSession());
        } catch (IllegalStateException e) {
            Log.e("DbAuthLog", "Error authenticating", e);
            return false; 
        }
        return true;
    }
    return false;
}

private static boolean storeAuthToken(AndroidAuthSession session){
        String oauth2AccessToken = session.getOAuth2AccessToken();
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, "oauth2:");
        edit.putString(ACCESS_SECRET_NAME, oauth2AccessToken);
        boolean success = edit.commit();
        return success;
    }
private static AndroidAuthSession loadAuthToken(AndroidAuthSession session) {
    String key = prefs.getString(ACCESS_KEY_NAME, null);
    String secret = prefs.getString(ACCESS_SECRET_NAME, null);
    Log.i(TAG, "pulling stored data ");
    Log.i(TAG, "ACCESS_KEY_NAME " + key);
    Log.i(TAG, "secret(oauth2AccessToken) " + secret);

    if (key == null || secret == null || key.length() == 0 || secret.length() == 0){
        return session;
    }
    if (key.equals("oauth2:")) {//aiming for OAuth2 only
        session.setOAuth2AccessToken(secret);
        AccessTokenPair accessTokenPair = new AccessTokenPair(key, secret);
        session.setAccessTokenPair(accessTokenPair);
    }
    return session;
}

private static void clearAuth() {
    Editor edit = prefs.edit();
    edit.clear();
    edit.commit();
}

0 个答案:

没有答案