Мне нужно обновить токен на 401 - Не авторизован с помощью retrofit / okhttp3.Что не так с моим кодом?
MainActivity
OkHttpClient.Builder httpClient = new OkHttpClient.Builder ();
httpClient.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer " + hereToken).build();
return chain.proceed(request);
}
});
httpClient.authenticator(new TokenAuthenticator());
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ServiceAPI gerritAPI = retrofit.create(ServiceAPI.class);
Call<ClientDetails> call = gerritAPI.getRefreshTokenDetails();
call.enqueue(new Callback<ClientDetails>() {
@Override
public void onResponse(Call<ClientDetails> call, Response<ClientDetails> response) {
System.out.println("response code " + response.code());
}
@Override
public void onFailure(Call<ClientDetails> call, Throwable t) {
System.out.println("fail");
t.printStackTrace();
}
});
TokenAuthenticator
Request.BuildernewRequest = response.request (). newBuilder ();
accessToken = "Bearer tokenXXX";
return newRequest
.header("Authorization", accessToken)
.build();
}
Это всегда 401, токены верны!