4

My app gets some responce from server, and in that responce I also receive access token, which I have to use in my further operations, in general I managed to fetch this token from my responce and now I can see it in textView. But anyway I have to create smth like refreshing the following token, because I think that my users won't too happy to log in my app a lot of times, after current token will expire. So right now I have very serious question, like how to insert the following token, which I can get from my responce into my request:

@Headers({"Content-type: application/json",
        "Authorization: Bearer my token"})
@GET("/v1/message/list")
Call<ListOfMess> getInMess(@Query("type") int type, @Query("offset") int offset);

right now I have to insert the last token every half an hour, because I won't be able to get any data. I tried to insert the following token into my retrofit initialization:

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://server/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

But it didn't work properly. So after some period of time I got know that I can store the following token by sharedpreferences and than insert it everywhere, but I don't know how I can create it. I hope that you will help mw with the following problem. And also if you know the way of refreshing my token after some period of time by some request, I will be happy to see your answer)) Sorry for my bad language skills.

4
  • Your app typically would not be able to see inside the access token to find out when it expires. Rather, it would find out that a token is expired/invalid at some point when it presents it to the server. At that point, you'd have to reauthenticate from the app. The rest of your question is hard to follow. In the past I have stored access tokens as plain strings in shared preferences. Commented Jul 30, 2018 at 14:45
  • yes I agree with you, I think that I will create some responce for getting a new token in a period of time which will be smaller than 0,5 hour, but I can't understand how I can save the current token and then, how I can insert it into my interface?
    – Andrew
    Commented Jul 30, 2018 at 14:48
  • I don't understand what you are trying to do here. Commented Jul 30, 2018 at 14:49
  • I'm trying to insert the most fresh token into header in my interface, and refresh it when I will need it. Right now I have to insert my last toke by hand, but I would like to get the following token from succersful answer from my server, then insert it into my interface and then refresh it after a small period of time
    – Andrew
    Commented Jul 30, 2018 at 14:53

1 Answer 1

8
    //Save token here
    String token = "Some token From Server";
    SharedPreferences preferences = getActivity().getSharedPreferences("MY_APP",Context.MODE_PRIVATE);
    preferences.edit().putString("TOKEN",token).apply();


    //Retrieve token wherever necessary
    SharedPreferences preferences = getActivity().getSharedPreferences("MY_APP",Context.MODE_PRIVATE);
    String retrivedToken  = preferences.getString("TOKEN",null);//second parameter default value.
4
  • Great, here my question is : is it OK and secured to store access_token and refresh_token into shared-preference ? Commented Sep 16, 2019 at 9:50
  • I further mean: will I be able to access it once the app is closed and reOpen later? Commented Sep 16, 2019 at 10:37
  • Yes obviously, you can retrieve the token after you reopen the app Commented Sep 16, 2019 at 10:50
  • How about the security? Where does the plugin store secured item like token? Does it store in localStorage or file ? How does it secure my token on the device, please explain. Commented Sep 16, 2019 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.