OkHttp Authenticator sometimes does not call authenticate on HTTP 401

流过昼夜 提交于 2019-12-24 08:25:32

问题


I have an issue that OkHttp's Authenticator does not call the authenticate method in some cases of HTTP 401 errors. Interceptors are always called, but authenticator is sometimes called and sometimes not. Once it does not call it it will not call it for several minutes and all network calls will fail with HTTP 401. Then after 5-6 minutes it will "unstuck" and call the authenticate method again.

I use it for refreshing token. If the token expiration is set to one hour, there are no issues, but when we set the expiration to one minute (for testing) the authenticator stops working.

When quickly stepping through code in the debugger the authenticate method is entered, but event its first line is not executed, it just exits from the method. I have a log as the very first line which is not printed. I have tried both synchronised access and non-synchronised. The authenticator is written in Kotlin.

This is a continuation of issue described in another SO question. I have found out that it even does not work with one Retrofit instance with one OkHttp client, so I have created a new question.

This is happening with OkHttp 3.10.0/3.11.0, Retrofit 2.4.0, android compile SDK 27 and build tools 28.0.1. On real devices with Android 6 and 8.1, debug and release builds, so it looks like it does not

EDIT:

After more debugging I have found out that the authenticator works flawlessly with OkHttp 3.9.1 + Retrofit 2.3.0. Anything newer will not work for frequent HTTP 401 errors. Using OkHttp 3.10.0 or newer with Retrofit 2.3.0 does not work and also Retrofit 2.4.0 with OkHttp 3.9.1 does not work. Also other combinations of newer OkHttp+Retrofit do not work.

One more extra information is that I use the RxJava2 adapter for Retrofit 2 (com.squareup.retrofit2:adapter-rxjava2), always the same version as the Retrofit library. I am using RxJava2 2.1.9 and I have just tried the latest 2.2.2, still not working except for the old OkHttp+Retrofit combination.

来源:https://stackoverflow.com/questions/52831568/okhttp-authenticator-sometimes-does-not-call-authenticate-on-http-401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!