Why Retrofit Authenticator does not refresh token?
Why Retrofit Authenticator does not refresh token?
I am building an app with Retrofit , Dagger etc. and using Authenticator to refresh the token. I have set the token expiry time to 1 minute just to see if the Authenticator is triggered and it's not.
Below is my Authenticator implementation
public class TokenAuthenticator implements Authenticator {
private ServiceManagerHolder serviceHolder;
@Inject
public TokenAuthenticator(@NonNull ServiceManagerHolder serviceHolder) {
this.serviceHolder=serviceHolder;
}
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
if(serviceHolder.serviceManager() ==null)
{
return null;
}
if(response.code()==401) {
Request request = response.request();
serviceHolder.serviceManager().refreshAccessToken(GRANT_TYPE_REFRESH,
KikoConstants.AUTH_TOKEN_TIMEOUT, ANDROID_CLIENT_ID,
CLIENT_SECRET,
REFRESH_TOKEN)
.enqueue(new Callback<GetTokenResponse>() {
@Override
public void onResponse(Call<GetTokenResponse> call, retrofit2.Response<GetTokenResponse> response) {
Log.e("Authenticator refreshing token :::", "In progress");
//code to save the token
request.newBuilder().header("Authorization", "Bearer " + newAccessToken);
}
@Override
public void onFailure(Call<GetTokenResponse> call, Throwable t) {
Log.e("Error refreshing token",t.getMessage());
}
});
return response.request();
}else
{
return response.request();
}
}
}
Service holder here is a reference to the webservices implementation class
Any ideas what i'm doing wrong ?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.