1
我不知道如何使用我的應用程序重新啓動後保存的身份驗證令牌,所以我不需要再次進行身份驗證。Dropbox認證後重新啓動應用
/*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();
}
後來我用...
dpAPI.getSession().startAuthentication(ADLAppActivity.this);
,然後我上傳文件,所以一切工作正常,我。但是,重新啓動應用程序後,我不想再次進行身份驗證。我應該如何在SharedPref中使用保存的令牌?
我一直在挖這個答案的時間太長了。謝謝,傳感器! – Sipty