2017-03-16 25 views
0

我正在构建一个android应用程序,用户必须从Dropbox下载图片。但是每次用户都必须认证自己。我希望应用程序首次保存细节,之后不需要验证。代码如下:如何保存Dropbox用户的详细信息,让他在每次Android应用程序启动时都不进行身份验证?

protected void initialize_session(){ 
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
    AndroidAuthSession session = new AndroidAuthSession(appKeys); 
    mDBApi = new DropboxAPI<AndroidAuthSession>(session); 
    mDBApi.getSession().startOAuth2Authentication(Control_Gate.this); 
} 


protected void onResume() { 
    if (mDBApi.getSession().authenticationSuccessful()) { 
     try { 
      // Required to complete auth, sets the access token on the session 
      mDBApi.getSession().finishAuthentication();; 
      String accessToken = mDBApi.getSession().getOAuth2AccessToken(); 
     } catch (IllegalStateException e) { 
      Log.i("DbAuthLog", "Error authenticating", e); 
     } 
    } 
    super.onResume(); 
} 

这是为了让用户返回到应用程序。我知道解决方案必须在这两个,但我不知道如何保存凭据。

回答

1

将您的accessToken保存为共享首选项/ SQLite。

为前。

SharedPreferences sp = getSharedPreferences(
              "First_share_memory", Activity.MODE_APPEND); 
            // save in cache memory 
            sp.edit().putString("accesstoken", accessToken).commit(); 

和getDropboxAPI方法使用:

private DropboxAPI <AndroidAuthSession> getDropboxAPI() { 
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE); 
    mDBApi = new DropboxAPI<AndroidAuthSession>(session); 

SharedPreferences sharedpreferences = getSharedPreferences("First_share_memory", Activity.MODE_APPEND); 
String savedAccessToken = sharedpreferences.getString("accesstoken", "");// get previously saved accessToken 

if (!TextUtils.isEmpty(savedAccessToken)) { 
    mDBApi.getSession().setOAuth2AccessToken(savedAccessToken); 
} 

return mDBApi; 
} 

欲了解更多详情,请参阅裁判:

link

+0

非常感谢你的工作。只需要包括: else { mDBApi.getSession()。startOAuth2Authentication(Control_Gate.this); } onResume方法 –

+0

另一个问题,我通过pubnub发送消息到android应用程序。我已经在服务中编写了代码,但现在Android操作系统正在终止该服务。我也使用了START_STICKY,但它重新启动服务几次,我也错过了一些通知。它只有512MB内存。有没有另一种方法可以让手机的代码不会挂起? –

+0

请创建一个新的问题。根据SO标准,每个问题都有一个新的线程。有关详细信息,请阅读有关后台和前台服务的信息。 – 2017-03-17 05:07:46

1

保存令牌SharedPrefernce,然后相应地使用它。以下是相同的示例代码。 制作以下更改您的onResume功能:

protected void onResume() { 
     AndroidAuthSession session = mApi.getSession(); 
     setLoggedIn(mApi.getSession().authenticationSuccessful()); 
     if (session.authenticationSuccessful()) { 
      try { 
       // Mandatory call to complete the auth 
       session.finishAuthentication(); 

       // Store it locally in our app for later use 
       TokenPair tokens = session.getAccessTokenPair(); 
       storeKeys(tokens.key, tokens.secret); 
       setLoggedIn(true); 
      } catch (IllegalStateException e) { 
       showToast(getString(R.string.could_not_authenticate_with_dropbox) 
         + e.getLocalizedMessage()); 
      } 
     } 
     super.onResume(); 
    } 

添加storeKeys和clearKeys功能,节省值SharedPreferences

private void storeKeys(String key, String secret) { 
     // Save the access key for later 
     SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
     Editor edit = prefs.edit(); 
     edit.putString(ACCESS_KEY_NAME, key); 
     edit.putString(ACCESS_SECRET_NAME, secret); 
     edit.commit(); 
    } 

    private void clearKeys() { 
     SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
     Editor edit = prefs.edit(); 
     edit.clear(); 
     edit.commit(); 
    } 
private String[] getKeys() { 
     SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
     String key = prefs.getString(ACCESS_KEY_NAME, null); 
     String secret = prefs.getString(ACCESS_SECRET_NAME, null); 
     if (key != null && secret != null) { 
      String[] ret = new String[2]; 
      ret[0] = key; 
      ret[1] = secret; 
      return ret; 
     } else { 
      return null; 
     } 
    } 

和初始化像下面您的会话:

public AndroidAuthSession buildSession() { 
     AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET); 
     AndroidAuthSession session; 

     String[] stored = getKeys(); 
     if (stored != null) { 
      AccessTokenPair accessToken = new AccessTokenPair(stored[0], 
        stored[1]); 
      session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE, 
        accessToken); 
     } else { 
      session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE); 
     } 
     return session; 
    } 

编辑:添加这三个常量,你可以评论setLoggedIn(true)的调用;

final static private String ACCOUNT_PREFS_NAME = "prefs"; 
final static private String ACCESS_KEY_NAME = "ACCESS_KEY"; 
final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET"; 
+0

什么是ACCOUNT_PREFS_NAME,ACCESS_KEY_NAME和ACCESS_SECRET_NAME?那里有错误 –

+0

什么是setLoggedIn()和getKeys()?他们的方法是由Android提供的吗?使用它们时我也会遇到错误 –

相关问题