2013-09-16 8 views
1

Je ne sais pas comment utiliser mon jeton d'authentification enregistré après le redémarrage de mon application, je n'ai donc pas besoin de m'authentifier à nouveau.Authentification Dropbox après redémarrage Application

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

} 

Plus tard, je l'utilise ...

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

puis je télécharger un fichier, pour que tout fonctionne bien pour moi. Mais, après avoir redémarré App, je ne veux plus m'authentifier. Comment dois-je utiliser le jeton enregistré dans SharedPref ???

Répondre

1

Veuillez vérifier this answer. Au lieu d'appeler le dpAPI.getSession().startAuthentication(ADLAppActivity.this);, vous devez appeler session.setOAuth2AccessToken(RESTORED_TOKEN); avec votre jeton restauré à partir des préférences.

+0

J'ai creusé beaucoup trop longtemps pour cette réponse. Merci, sénateur! – Sipty

Questions connexes