gpt4 book ai didi

android - 在 OkHttp 拦截器中刷新 token

转载 作者:行者123 更新时间:2023-12-01 01:56:38 25 4
gpt4 key购买 nike

我正在编写一个拦截器,这样当我得到 401 时,我使用刷新 token 在后台获取新的访问 token ,然后将该 token 添加到请求中。

代码示例:

    @Override
public Response intercept(final Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);

if (response.code() != 200) {
try {
new TokenRequestTask(tokenRequest, request, OkHttpresponse, chain).execute().get(5000, TimeUnit.MILLISECONDS);
response = chain.proceed(request);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
return response;
}

在异步任务中 执行后 ,我正在通过添加收到的新身份验证 token 来更改请求的 header 。但问题是输出不符合预期。该函数正在返回旧响应,因为之后异步任务的结果。理想情况下,它应该等待异步任务结果然后返回但没有发生。 如何实现 ?

最佳答案

我在另一个线程上为 getRefreshToken 使用 Protocol Buffer 并成功地做到了!这是我的代码:

public class IgapRetrofitInterceptor implements Interceptor {

private boolean isRefreshing;

private TokenContainer tokenContainer = TokenContainer.getInstance();

@NotNull
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = chain.request();

Request.Builder builder = request.newBuilder();
builder.header("Authorization", tokenContainer.getToken());
builder.header("Content-Type", "application/json");
builder.method(original.method(), original.body());

String token = tokenContainer.getToken();

request = builder.build();
Response response = chain.proceed(request);

if (response.code() == 401) {
synchronized (this) {
Log.e("refreshToken", "Failed " + request.toString() + " with token -> " + tokenContainer.getToken());

String currentToken = tokenContainer.getToken();

if (currentToken != null && currentToken.equals(token)) {
try {
getRefreshToken(); //aysnc
} catch (InterruptedException e) {
e.printStackTrace();
}
}

if (tokenContainer.getToken() != null) {
builder.header("Authorization", tokenContainer.getToken());
request = builder.build();
Log.e("refreshToken", "Send " + request.toString() + " again with new token -> " + tokenContainer.getToken());
Log.e("refreshToken", "--------------------------------------------------------------------------------");
return chain.proceed(request);
}
}
}

Log.i("refreshToken", "req 200 with token -> " + tokenContainer.getToken());

return response;
}

public synchronized void getRefreshToken() throws InterruptedException {
if (!isRefreshing) {

isRefreshing = true;

Log.e("refreshToken", "Refreshing token...");
tokenContainer.getRefreshToken(() -> {
synchronized (IgapRetrofitInterceptor.this) {
isRefreshing = false;
Log.e("refreshToken", "Proto response on success and token updated with token -> " + tokenContainer.getToken());
IgapRetrofitInterceptor.this.notifyAll();
}
});
}

Log.e("refreshToken", "lock thread -> " + android.os.Process.getThreadPriority(android.os.Process.myTid()) + this.toString());
this.wait();
Log.e("refreshToken", "unlock thread -> " + android.os.Process.getThreadPriority(android.os.Process.myTid()) + this.toString());

}
}

关于android - 在 OkHttp 拦截器中刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40068293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com