Android dropbox api needs re-authentication

别来无恙 提交于 2019-12-24 01:42:38

问题


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.


回答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);
    }
}


来源:https://stackoverflow.com/questions/14810896/android-dropbox-api-needs-re-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!