2

In my application, i use dropbox api to keep some files, it's ok. After authentication i close the app and re-launch app. It needs re-authentication each time i opened the application.I want the application to remember my session.

ChuKoNu
  • 477
  • 4
  • 20

1 Answers1

1

Dropbox tutorial suggest storing the authentication token as SharedPreferences, so you could restore it later.

You can see an example application in dropbox SDK located in \dropbox-android-sdk-1.6\examples\DBRoulette.

In the activity's onCreate() method check if preference is stored and if it is then instean of calling authentication window use session.setOAuth2AccessToken(RESTORED_TOKEN);

Sample code to do this:

public void onCreate() {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

    String token = getTokenFromPreferences();
    if (token != null) {
        session.setOAuth2AccessToken(token);
    } else {
        mDBApi.getSession().startOAuth2Authentication(MyActivity.this);
    }
}
Tautvydas
  • 2,027
  • 3
  • 25
  • 38