sagar suri
sagar suri

Reputation: 4731

How to check if access token is expired or not in android

I am using oauth2.0 system to make network calls. I have the token expire time i.e: 2096 sec. Now before I make any network call using Volley. I want to check whether the token is expired or not. How can I calculate that using the expire time provided to me through the API ?

Upvotes: 0

Views: 1728

Answers (1)

Sunil Soni
Sunil Soni

Reputation: 443

What you can try is save the time when you get the token in the shared preferences as the "refresh_time" and when you make a network call. just check using the condition

    float refresh_time=Calendar.getInstance().getTimeInMillis();
    saveRefreshTime(refresh_time);
    //when you refresh the token use the code written above

    float current_time=Calendar.getInstance().getTimeInMillis();


    float newTime=current_time-refresh_time/1000;

    float bufferTime=100;
    if(newTime<(2096-bufferTime)){

        //DO your network call
    }
    else{
        //refresh the token

    }

the buffer_time can be anytime like 100 seconds for safer side

Upvotes: 3

Related Questions