From cb9dc7e777020e85db0aa3c386eb038053ee61da Mon Sep 17 00:00:00 2001 From: zhouxin Date: Sat, 24 Jul 2021 17:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBUG=EF=BC=9A=E5=BD=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=ADrefreshToken=E8=BF=87=E6=9C=9F?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/AliYunDriverClient.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java b/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java index 5f938c5..31a8918 100644 --- a/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java +++ b/src/main/java/com/github/zxbu/webdavteambition/client/AliYunDriverClient.java @@ -42,7 +42,14 @@ public class AliYunDriverClient { @Override public Request authenticate(Route route, Response response) throws IOException { 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 refreshToken = (String) JsonUtil.getJsonNodeValue(refreshTokenResult, "refresh_token"); Assert.hasLength(accessToken, "获取accessToken失败"); @@ -178,6 +185,16 @@ public class AliYunDriverClient { 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() { String refreshTokenPath = aliYunDriveProperties.getWorkDir() + "refresh-token"; Path path = Paths.get(refreshTokenPath);