使用拦截器,我尝试更新访问令牌。但是代码不起作用...如何使用 http 客户端拦截器更新访问令牌?
try (CloseableHttpClient httpClient = HttpClients.custom().
addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "a1", (request, scope, chain) -> {
ClassicHttpResponse response = chain.proceed(request, scope);
if (response.getCode() == HttpStatus.SC_UNAUTHORIZED) {
userAuthBean.updateAccessTokenFromAPI();
request.addHeader("Authorization", "Bearer " + userAuthBean.getAccessToken());
chain.proceed(request, scope);
}
return response;
})
.build())
{
HttpGet request = new HttpGet(uri);
request.addHeader("Authorization", "Bearer " + userAuthBean.getAccessToken());
try (CloseableHttpResponse response = httpClient.execute(request)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println(result);
}
} catch (IOException | ParseException ioException) {
ioException.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return result;