Reputation: 35
I am building an android app where the user have to download pictures from Dropbox. However every time, the user has to authenticate himself. I want the application to save the details first time and no authentication needed afterwards. The codes are below:
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();
}
This is for returning the user to the app. I know that the the solution must be in these two but I can't figure how to save the credentials.
Upvotes: 0
Views: 72
Reputation: 1000
Save your token to SharedPrefernce and then use it accordingly. Below is the sample code for the same. Make following changes in your onResume function:
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();
}
Add storeKeys and clearKeys function to save values in 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;
}
}
And initialize your session like below:
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;
}
Edit: Add these three constants and you can comment the call of 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";
Upvotes: 1
Reputation:
Save your accessToken in shared preference/SQLite.
for ex.
SharedPreferences sp = getSharedPreferences(
"First_share_memory", Activity.MODE_APPEND);
// save in cache memory
sp.edit().putString("accesstoken", accessToken).commit();
and use this in getDropboxAPI method:
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;
}
For more details see the ref:
Upvotes: 1