OAuth instance state in Android

后端 未结 6 1145
眼角桃花
眼角桃花 2020-12-08 08:49

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

6条回答
  •  执笔经年
    2020-12-08 09:10

    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.

提交回复
热议问题