gpt4 book ai didi

java - 当 REST API 在 Retrofit2 RxJava 中返回 401 时刷新访问 token

转载 作者:行者123 更新时间:2023-12-02 09:42:15 26 4
gpt4 key购买 nike

我在 REST API 中有以下端点:


public interface AutomoticzAPI {

@POST("/api/beacon_auth/login")
Single<LoginResponse> login(@Body LoginRequest request);

@GET("/api/system/ws_devices")
Single<WSDevicesResponse> wsDeviceList(@Header("Authorization") String tokenHeader);

}

当我调用 login 端点时,作为响应,我收到了保存到 ClientSession 持有者对象中的访问 token 。稍后我可以从 ClientSession 检索 token ,用于调用服务器的 protected 资源:

        api.login(ClientSession.getInstance().getLoginRequest(login, password))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(loginResponse -> {
String accessToken = loginResponse.getAccessToken();
ClientSession.getInstance().setAccessToken(accessToken);
view.onLoginSuccess();
}, throwable -> {
RetrofitException exception = (RetrofitException) throwable;
if (exception.getKind().equals(RetrofitException.Kind.HTTP)){
view.onLoginFailed(exception.getMessage());
} else if(exception.getKind().equals(RetrofitException.Kind.NETWORK))
{
view.onLoginFailed("Network error...");
} else {
view.onLoginFailed("Unknown error occurred...");
}
});

当我调用 wsDeviceList 端点时,服务器可能返回 401 HTTP 响应代码和带有错误代码和消息的 json 正文:

{
"code": "EXPIRED-TOKEN",
"message": "Token expired"
}

如果发生这种情况,我想再次调用登录端点以获取新的访问 token 。这是到目前为止我的代码:

    ClientSession clientSession = ClientSession.getInstance();
String token = "Bearer "+clientSession.getAccessToken();
String url = ClientSession.getInstance().getUrl();
AutomoticzAPI api = NetworkManager.getApiClient(url);
api.wsDeviceList(token)
.retryWhen(throwableFlowable -> throwableFlowable.flatMap(
new Function<Throwable, Publisher<?>>() {
@Override
public Publisher<?> apply(Throwable throwable) throws Exception {
RetrofitException exception = (RetrofitException) throwable;
if (exception.isUnauthorizedError()){
return relogin(api, clientSession.getLoginRequest());
}
return (Publisher<?>) throwable;
}
}
))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(wsDevicesResponse -> {
view.onDeviceListLoaded(wsDevicesResponse.getWsdevices());
}, throwable -> {
RetrofitException exception = (RetrofitException) throwable;
view.onError(exception);
});
}

public Publisher<?> relogin(AutomoticzAPI api, LoginRequest loginRequest){
return (Publisher<?>) api.login(loginRequest)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(loginResponse -> {
String accessToken = loginResponse.getAccessToken();
ClientSession.getInstance().setAccessToken(accessToken);
}, throwable -> {
RetrofitException exception = (RetrofitException) throwable;
view.onError(exception);
});
}

但是当执行relogin方法时,我的程序崩溃了。我不精通 RxJava,可能做错了。如何调用login来刷新访问 token ,然后再次调用wsDeviceList

最佳答案

使用改造的 Authenticator API 并在此调用访问 token api 内获取访问 token 并使用此访问 token 重新尝试失败的 API 调用。

关于java - 当 REST API 在 Retrofit2 RxJava 中返回 401 时刷新访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971682/

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