Reputation: 1099
I am developing an Android client with GAE backend and I am using the phone accounts for authentication.
After I retrieve the authentication cookie from GAE, I can make authenticated calls to the server, but as soon as the application is closed, when the app re-launches, I have to run the auth proces again, ask for e new cookie. Is there a way to store that Cookie object and check for it after the first time the auth happens so I don't have to do it every time the app launches?
Upvotes: 1
Views: 509
Reputation: 1064
If you want to leverage functionality of the already implemented provider you should use AccountManager
:
AccountManager am = AccountManager.get(MyActivity.this);
Account[] aArray = am.getAccountsByType(ACCOUNT_TYPE);
/* choose correct account from the resulting array */
/* ex. Account a = aArray[0]; */
try {
String token = am.blockingGetAuthToken(a, Constants.AUTHTOKEN_TYPE, false);
} catch (OperationCanceledException e1) {
e1.printStackTrace();
} catch (AuthenticatorException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
You should override getTokenType() method of your account authenticator to return GAE authentication cookie in the result bundle under the key AccountManager.KEY_AUTHTOKEN
. Refer to AccountManager class description for detailed authentication procedure.
Upvotes: 0
Reputation: 31503
Sounds like a good use for the SharedPreferences
PreferenceManager.getDefaultSharedPreferences().edit().putString("cookie", myCookie).commit();
String myCookie = PreferenceManager.getDefaultSharedPreferences.getString("cookie", null);
Upvotes: 2