修复BUG:当文件中refreshToken过期时,无法正常启动

This commit is contained in:
zhouxin 2021-07-24 17:22:00 +08:00
parent 45cdc14920
commit cb9dc7e777
1 changed files with 18 additions and 1 deletions

View File

@ -42,7 +42,14 @@ public class AliYunDriverClient {
@Override @Override
public Request authenticate(Route route, Response response) throws IOException { public Request authenticate(Route route, Response response) throws IOException {
if (response.code() == 401 && response.body() != null && response.body().string().contains("AccessToken")) { if (response.code() == 401 && response.body() != null && response.body().string().contains("AccessToken")) {
String refreshTokenResult = post("https://websv.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", readRefreshToken())); String refreshTokenResult;
try {
refreshTokenResult = post("https://websv.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", readRefreshToken()));
} catch (Exception e) {
// 如果置换token失败先清空原token文件再尝试一次
deleteRefreshTokenFile();
refreshTokenResult = post("https://websv.aliyundrive.com/token/refresh", Collections.singletonMap("refresh_token", readRefreshToken()));
}
String accessToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "access_token"); String accessToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "access_token");
String refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token"); String refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token");
Assert.hasLength(accessToken, "获取accessToken失败"); Assert.hasLength(accessToken, "获取accessToken失败");
@ -178,6 +185,16 @@ public class AliYunDriverClient {
return aliYunDriveProperties.getUrl() + url; return aliYunDriveProperties.getUrl() + url;
} }
private void deleteRefreshTokenFile() {
String refreshTokenPath = aliYunDriveProperties.getWorkDir() + "refresh-token";
Path path = Paths.get(refreshTokenPath);
try {
Files.delete(path);
} catch (IOException e) {
e.printStackTrace();
}
}
private String readRefreshToken() { private String readRefreshToken() {
String refreshTokenPath = aliYunDriveProperties.getWorkDir() + "refresh-token"; String refreshTokenPath = aliYunDriveProperties.getWorkDir() + "refresh-token";
Path path = Paths.get(refreshTokenPath); Path path = Paths.get(refreshTokenPath);