2013-02-11 67 views
2

在我的应用程序中,我使用Dropbox API保留一些文件,没关系。身份验证后,我关闭了应用程序并重新启动应用程序。每次打开应用程序时都需要重新验证。我希望应用程序记住我的会话。Android Dropbox API需要重新认证

回答

1

Dropbox tutorial建议将身份验证令牌存储为SharedPreferences,以便稍后恢复。

您可以在位于\dropbox-android-sdk-1.6\examples\DBRoulette的Dropbox SDK中看到示例应用程序。

在活动的onCreate()方法检查,如果优先存储,如果它是那么instean调用的身份验证窗口中使用session.setOAuth2AccessToken(RESTORED_TOKEN);

示例代码做到这一点:

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