本文介绍了将Coroutine Runblock与Authenticator结合使用以处理401改造后的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Authenticator处理401响应.我所做的是

I am trying to use the Authenticator to handle 401 response. What I have done is

fun provideAccessTokenAuthenticator(
    mainApiServiceHolder: MainApiServiceHolder,
    preferences: SharedPreferences
) = object : Authenticator {
    override fun authenticate(route: Route?, response: Response): Request? {
        val accessToken = preferences.getString(ACCESS_TOKEN, null)
        if (!isRequestWithAccessToken(response) || accessToken == null) {
            return null
        }
        synchronized(this) {
            val newAccessToken = preferences.getString(ACCESS_TOKEN, null)!!
            // Access token is refreshed in another thread.
            if (accessToken != newAccessToken) {
                return newRequestWithAccessToken(response.request, newAccessToken)
            }

            // Need to refresh an access token
            val refreshTokenResponse = runBlocking {
                Log.d("zzzzzzzzzz", "refresh token is running")
                mainApiServiceHolder.mainApiService?.refreshToken(
                    "refresh_token",
                    preferences.getString(REFRESH_TOKEN, null)!!,
                    AuthRepository.CLIENT_ID,
                    AuthRepository.CLIENT_SECRET
                )
            }
            Log.d("zzzzzzzzzz", refreshTokenResponse?.body()?.access_token!!)
            return if (refreshTokenResponse?.isSuccessful!!) {
                Log.d("zzzzzzzzzz", "refresh token is successful")
                newRequestWithAccessToken(
                    response.request,
                    refreshTokenResponse.body()?.access_token!!
                )
            } else {
                Log.d("zzzzzzzzzz", "refresh token is unsuccessful")
                response.request.newBuilder().header("Content-Type", "application/json").build()
            }
        }
    }

现在,当有401响应时,它将被调用.还触发了刷新令牌调用(从Log).但是,它永远不会在refreshTokenResponse中得到结果,并且此后什么也没有发生.我认为使用runBlock是错误的方式.该API是

Now, it gets called when there is a 401 response. The refresh token call is also fired (from Log). However, it never gets the result in the refreshTokenResponse and nothing happens after that. I think its a wrong way of using runBlock. The api is

@FormUrlEncoded
@POST("/api/auth/token/")
suspend fun refreshToken(
    @Field("grant_type") grant_type: String,
    @Field("refresh_token") refresh_token: String,
    @Field("client_id") client_id: String,
    @Field("client_secret") client_secret: String
): Response<LoginResponse>

任何帮助将不胜感激.谢谢

Any help would be really appreciated. Thanks

推荐答案

在Retrofit API中,考虑使用同步的致电.我最幸运的是避免在Authenticator中使用协程.

In the Retrofit API, consider replacing your async runBlocking{} suspend fun with a synchronous Call. I had the most luck avoiding the use of coroutines inside the Authenticator.

我遇到了同样的问题.令牌请求直接进入了一个黑洞.该应用程序冻结.该请求再也没有看到.没错,没事.

I was having the same problem. The token request went straight into a black hole. The app freezed. The request was never seen again. No error, no nothing.

但是在应用程序的其他任何地方,暂停的乐趣又回来了.从ViewModels,从WorkManager,它每次都起作用.但是从身份验证器,永远不会.身份验证器出了什么问题?身份验证器以这种方式起作用的特殊之处是什么?

But everywhere else in the app, the suspend fun came back just fine. From ViewModels, from WorkManager, it worked every time. But from the Authenticator, never. What was wrong with the Authenticator? What was special about the Authenticator that made it act this way?

然后,我用一个简单的替换了runBlocking {}协程致电.这次,请求又回来了,令牌也毫不费力地到达了.

Then I replaced the runBlocking{} coroutine with a straightforward Call. This time, the request came back and the token arrived without a fuss.

我使API正常工作的方式如下:

The way I got the API to work looked like this:

@FormUrlEncoded
@POST("token")
fun refreshTokenSync(
    @Field("refresh_token") refreshToken: String,
): Call<RefreshMyTokenResponse>

然后,在身份验证器中

 val call = API.refreshTokenSync(refreshToken)
 val response = call.execute().body()

我希望这可以帮助遇到相同问题的其他人.您可能会收到来自Android Studio的警告,说这是不适当的阻止呼叫.不用管它.

I hope this helps someone else who ran into the same issue. You may receive a warning from Android Studio that this is an inappropriate blocking call. Don't pay it any nevermind.

这篇关于将Coroutine Runblock与Authenticator结合使用以处理401改造后的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 19:08