I\'m trying to use OAuth in an Android app. I have it working correctly but have sometimes run into a problem during the authentication phase. In Android, I launch the brows
I solved this by persisting the provider object to a file. I'm using the signpost library and both the provider and consumer are serializable.
protected void loadProvider()
{
FileInputStream fin = this.openFileInput("provider.dat");
ObjectInputStream ois = new ObjectInputStream(fin);
this.provider = (DefaultOAuthProvider) ois.readObject();
ois.close();
consumer = this.provider.getConsumer();
}
protected void persistProvider()
{
FileOutputStream fout = this.openFileOutput("provider.dat", MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(this.provider);
oos.close();
}
I call persist provider just before launching the browser view intent for authentication, and I restore the provider in onResume() just before calling provider.retrieveAccessToken(). If you call persistProvider() and loadProvider() in a couple more locations, you can also have it save the proper tokens post-authentication. This would remove the need to re-authenticate (as long as the token is valid).
I do still wish I knew which fields in the provider class are actually needed to persist. Might be kind of slow to serialize the entire object.