Dropbox在重新启动Application后进行身份验证

时间:2013-09-16 16:38:50

标签: android authentication dropbox

我不知道在重新启动应用程序后如何使用我保存的身份验证令牌,所以我不需要再次进行身份验证。

/*DROPBOX  ==========================*/
 private String APP_KEY= "key";
 private String APP_SECRET= "secret";

 AppKeyPair appKeys;
 AndroidAuthSession session;
 DropboxAPI<AndroidAuthSession> dpAPI;

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readings_main);

   //get dropbox keys
    SharedPreferences sharedPref = getSharedPreferences(getString(R.string.dp_key_token), Context.MODE_PRIVATE);

      // if i use these 2 lines i get exception that my key isn´t set in manifest, and thats true because in manifest i have the first key, not hte generated after auth.
  //  APP_KEY= sharedPref.getString("key", "key");
  //  APP_SECRET= sharedPref.getString("secret", "secret");


appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    // setup dropbox session
    session = new AndroidAuthSession(appKeys, AccessType.DROPBOX);
    dpAPI= new DropboxAPI<AndroidAuthSession>(session);


}


 protected void onResume() {
    super.onResume();

     if (dpAPI.getSession().authenticationSuccessful()) {
            try {
                // Required to complete auth, sets the access token on the session
                dpAPI.getSession().finishAuthentication();
                AccessTokenPair tokens = dpAPI.getSession().getAccessTokenPair();
                //store keys in sharedpreferences       ;
                storeKeys(tokens.key, tokens.secret);

            } catch (IllegalStateException e) {
                Log.i("DbAuthLog", "Error authenticating", e);
            }
        }
}

public boolean storeKeys(String key, String secret) {

   SharedPreferences sharedPref = getSharedPreferences(getString(R.string.dp_key_token), Context.MODE_PRIVATE);
   SharedPreferences.Editor editor = sharedPref.edit();
   editor.putString("key", key);
   editor.putString("secret", secret);
   return editor.commit();

}

后来我用......

dpAPI.getSession().startAuthentication(ADLAppActivity.this);

然后我上传了一个文件,所以一切正常。但是,重新启动App后我不想再次进行身份验证。我应该如何在SharedPref中使用保存的令牌???

1 个答案:

答案 0 :(得分:1)

请检查this answer

您应该使用从偏好设施恢复的令牌来调用dpAPI.getSession().startAuthentication(ADLAppActivity.this);,而不是致电session.setOAuth2AccessToken(RESTORED_TOKEN);