问题
I have two different Retrofit
instances for two different APIs. I also have two different OkHttp3
clients but they share the same Authenticator
because the auth token is the same for both APIs.
The problem is that when the token expires then sometimes (but almost always) one of the Retrofit
/OkHttpClient
objects will not call the Authenticator
's authenticate
method on HTTP 401. It will happily make API calls and all will end with 401 and the authenticator is completely ignored. In this case, when the second Retrofit
makes an API call it will receive 401 and the authenticate
is called, token is refreshed, and everything is back to normal. Including the second Retrofit
API, which was failing all calls with 401.
Bigger problem is that this behaviour is random but the failure is more on than half of token expirations. We have tried two Retrofit
instances with one shared OkHttp3
client, then two OkHttp3
clients with shared interceptors and authenticator and finally also not-shared interceptors and authenticators. And even in the last case, with everything having its own instance, the authenticate is not called on some 401 errors.
Interceptor
s work, they inject the old token into headers, just the Authenticator
's authenticate
method is not called on every HTTP 401 error.
When stepping through debugger this never happens and the authenticate is always called. This looks like a multi-threading issue to me. Instant run is disabled in the Android Studio. Running on real device with Android 6.
EDIT: OkHttp 3.10.0, Retrofit 2.4.0, android compile SDK 27 and build tools 28.0.1
来源:https://stackoverflow.com/questions/52811938/okhttp-authenticator-sometimes-does-not-call-authenticate-with-multiple-retrofit