Peter Birdsall
Peter Birdsall

Reputation: 3425

How store credential from OAuth2 in shared preferences

I have several activities that connect to Google, I only want to connect to Google once and share the credential throughout the app. I have an activity that authenticates to Google via OAuth2. It returns a credential as follows:

private GoogleAccountCredential credential;

...

credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);

Is there a way to put it into Shared Preferences. I've tried all the obvious types, i.e. parcelable, etc. and I've found the credential can be put into Shared Preferences, but can't find a getter to convert it back to GoogleAccountCredential. I had even tried making it an object, but again issues with the getter converting it back to the GoogleAccountCredential type.

If Share Preference isn't capable, it there an alternative?

Thanks in advance for you time and help.

Upvotes: 1

Views: 1169

Answers (1)

Gaurav Agarwal
Gaurav Agarwal

Reputation: 19102

You might want to use the AccountManager API in Android. Account Manager is capable of storing the OAuth token and does the job for all Google Apps in Android. You may follow the blog here.

Upvotes: 6

Related Questions