相关疑难解决方法(0)

Okhttp Authenticator多线程

OkHttp在我的Android应用程序中使用了几个异步请求.所有请求都需要使用标头发送令牌.有时我需要使用RefreshToken刷新令牌,所以我决定使用OkHttp's Authenticatorclass.

当两个或多个异步请求同时从服务器获得401响应代码时会发生什么?authenticate()是为每个请求调用Authenticator的方法,还是只为第一个获得401的请求调用一次?

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException
{                
    return null;
}
Run Code Online (Sandbox Code Playgroud)

如何只刷新一次令牌?

authentication multithreading android okhttp

8
推荐指数
1
解决办法
1984
查看次数

OkHttp和Retrofit,用并发请求刷新令牌

在我的应用程序中,我实现了Retrofit来调用WebServices,我使用OkHttp来使用Interceptor和Authenticator.有些请求需要令牌,我已经实现了Authenticator接口来处理刷新(遵循官方文档).但我有以下问题:在我的应用程序中不时,我必须立即调用多个请求.因此,对于其中一个我将有401错误.

这是我的请求调用代码:

public static <S> S createServiceAuthentication(Class<S> serviceClass, boolean hasPagination) {

        final String jwt = JWT.getJWTValue(); //Get jwt value from Realm

        if (hasPagination) {
            Gson gson = new GsonBuilder().
                    registerTypeAdapter(Pagination.class, new PaginationTypeAdapter()).create();

            builder =
                    new Retrofit.Builder()
                            .baseUrl(APIConstant.API_URL)
                            .addConverterFactory(GsonConverterFactory.create(gson));
        }

        OkHttpClient.Builder httpClient =
                new OkHttpClient.Builder();

        httpClient.addInterceptor(new AuthenticationInterceptor(jwt));
        httpClient.authenticator(new Authenticator() {
            @Override
            public Request authenticate(Route route, Response response) throws IOException {
                if (responseCount(response) >= 2) {
                    // If both the original call and the call with refreshed token …
Run Code Online (Sandbox Code Playgroud)

android retrofit2 okhttp3

8
推荐指数
1
解决办法
1992
查看次数

如何使用 okhttp 身份验证器一次多次请求刷新令牌

我在我的项目中使用改造进行网络连接。问题是我必须从我的第一个活动中调用 2 个请求。它工作正常,但是当访问令牌过期时,它必须刷新令牌。我已经使用 okhttp Authenticator 实现了一个调用。但是它多次调用并且这个错误也显示出来too many followup request 21

编辑 我更新了 TokenAuthenticator 类并添加了 synchronized()。但它是从if (originalRequest.header("Authorization") != null) return null. 我正在关注这个答案/sf/answers/3675918571/

如果我要删除if (originalRequest.header("Authorization") != null) return null此行,则它可以正常工作,但在日志报告中,我看到它多次调用刷新令牌。我怎样才能避免这种多次调用?

这是我的 Authenticator 类

class TokenAuthenticator : Authenticator {

private val refreshTokenGrandType = "refresh_token"
private var oldToken: String? = null
private var newToken: String? = null

override fun authenticate(route: Route?, response: Response?): Request? {
    oldToken = SharedPreferenceManager(MainApplication.applicationContext()).getToken()
    if (response == null) return null
    val originalRequest = response.request()
    if …
Run Code Online (Sandbox Code Playgroud)

android oauth kotlin retrofit okhttp

5
推荐指数
0
解决办法
4626
查看次数

使用 Coroutine runblock 和 Authenticator 处理来自改造的 401 响应

我正在尝试使用身份验证器来处理 401 响应。我所做的是

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( …
Run Code Online (Sandbox Code Playgroud)

android okhttp retrofit2 kotlin-coroutines

5
推荐指数
1
解决办法
1312
查看次数