OkHttp身份验证器刷新令牌循环

时间:2019-11-30 16:55:13

标签: android kotlin okhttp

我在OkHttp Authenticator中有这段代码来从服务器返回401响应时刷新令牌,但是一旦返回401,则每次使用新的访问和刷新令牌时该代码就开始循环而不会结束,任何想法都是错误吗?

override fun authenticate(route: Route?, response: Response): Request? {
    val refreshToken = tokenManager.getRefreshTokenFromSharedPrefs()

    val refreshTokenRequest = NetworkRefreshTokenRequest(refreshToken!!)

    val tokenResponse = authApi.api.refreshToken(refreshTokenRequest).execute()

    return when(tokenResponse.isSuccessful){
        true -> {
            val tokens = tokenResponse.body()!!

            Timber.tag("AUTHENTICATOR TOKEN: ").i(tokens.token)
            Timber.tag("AUTHENTICATOR R_TOKEN: ").i(tokens.refreshToken)

            tokenManager.saveTokensToSharedPrefs(tokens.token, tokens.refreshToken)
            response.request().newBuilder().header("Authorization", "Bearer ${tokens.token}").build()
        }
        else -> null
    }

}```

0 个答案:

没有答案