1

i dont know how to use my saved authentication token after restart of my application, so i don´t need to authenticate again.

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

}

Later i use...

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

and then i upload a file, so everything works fine for me. But, after restart App i don´t want to authenticate again. How should i use the saved Token in SharedPref???

user2784676
  • 105
  • 1
  • 12

1 Answers1

1

Please check this answer.

Instead of calling dpAPI.getSession().startAuthentication(ADLAppActivity.this); you should call session.setOAuth2AccessToken(RESTORED_TOKEN); with your token restored from preferences.

Community
  • 1
  • 1
Tautvydas
  • 2,027
  • 3
  • 25
  • 38