1

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

/*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);

然后我上传一个文件,所以一切对我来说都很好。但是,重新启动应用程序后,我不想再次进行身份验证。我应该如何使用 SharedPref 中保存的令牌???

4

1 回答 1

1

请检查这个答案

dpAPI.getSession().startAuthentication(ADLAppActivity.this);您应该session.setOAuth2AccessToken(RESTORED_TOKEN);使用从首选项中恢复的令牌进行调用,而不是调用。

于 2014-02-19T00:03:00.773 回答