5

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

RmK
  • 1,408
  • 15
  • 28

1 Answers1

2

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()

Community
  • 1
  • 1
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • Thanks for the link. You have almost asked everything I wanted in there. In this case, I'm using `GoogleAuthUtil` and storing the retrieved token as a string ( according to the sample code in developer.android.com). Now I thought I had to pass around this token manually everytime I have to make a network call, but from your link I found something called `GoogleAuthUtil.getToken()` which I believe I can use in any activity. Let me play around with that, will return back and mark your answer as the right one if I succeed. Thanks !! – RmK Aug 06 '14 at 06:35
  • Yes, actually you do not need to call `getToken()` at any point. You can just make use of these wrappers to make Google API's call. API will automatically request token on behalf of user, if its not there and persist it. It will re-obtain it automatically once its expires, if the user has already authorized the app. – Madhur Ahuja Aug 06 '14 at 06:36