Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice in storing and using a OAuth2 Token in Android?

I have an android app which talks witha Node.js backend, via REST api. We use OAuth token received from Google for authorization, and we have agreed on the flow in which I use the token in HTTP request everytime I make a request.

So, which is the best practice to store the token ? -

a) Store the token in SharedPreference, and use the same SharedPrefrence in the activity that makes the network calls.

OR

b) Store it using a POJO, and use getters and setters to retrieve and clear the token.

I have just started off with network related mini projects in Android. And hence this could be a very basic question. Any help is really appreciated.

Thanks

like image 771
RmK Avatar asked Nov 01 '22 19:11

RmK


1 Answers

How are you retrieving the token or doing Authorization?

If you are using GoogleAuthUtil or GoogleAccountCredential API's, the persistance of token is automatically managed by API's themselves.

You should not be required to do it manually.

See this question on which one to use:

Access to Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()

like image 163
Madhur Ahuja Avatar answered Nov 11 '22 18:11

Madhur Ahuja