This commit is contained in:
2025-11-05 22:53:55 +08:00
parent cddfde34df
commit 79a954a91f
2 changed files with 40 additions and 28 deletions

View File

@@ -75,36 +75,42 @@ public class TencentDocTokenServiceImpl implements ITencentDocTokenService {
// 先尝试使用refresh_token刷新
if (redisCache.hasKey(refreshTokenKey)) {
String refreshToken = redisCache.getCacheObject(refreshTokenKey).toString();
try {
JSONObject tokenInfo = TencentDocApiUtil.refreshAccessToken(
tencentDocConfig.getAppId(),
tencentDocConfig.getAppSecret(),
refreshToken,
tencentDocConfig.getRefreshTokenUrl()
);
if (tokenInfo != null && tokenInfo.containsKey("access_token")) {
String newAccessToken = tokenInfo.getString("access_token");
String newRefreshToken = tokenInfo.getString("refresh_token");
Integer expiresIn = tokenInfo.getIntValue("expires_in");
Object refreshTokenObj = redisCache.getCacheObject(refreshTokenKey);
if (refreshTokenObj != null) {
String refreshToken = refreshTokenObj.toString();
try {
JSONObject tokenInfo = TencentDocApiUtil.refreshAccessToken(
tencentDocConfig.getAppId(),
tencentDocConfig.getAppSecret(),
refreshToken,
tencentDocConfig.getRefreshTokenUrl()
);
// 保存新的token
saveToken(newAccessToken, newRefreshToken, expiresIn);
log.info("成功刷新访问令牌");
return newAccessToken;
if (tokenInfo != null && tokenInfo.containsKey("access_token")) {
String newAccessToken = tokenInfo.getString("access_token");
String newRefreshToken = tokenInfo.getString("refresh_token");
Integer expiresIn = tokenInfo.getIntValue("expires_in");
// 保存新的token
saveToken(newAccessToken, newRefreshToken, expiresIn);
log.info("成功刷新访问令牌");
return newAccessToken;
}
} catch (Exception e) {
log.warn("使用refresh_token刷新失败: {}", e.getMessage());
// refresh_token可能已过期清除它
redisCache.deleteObject(refreshTokenKey);
}
} catch (Exception e) {
log.warn("使用refresh_token刷新失败尝试重新获取授权: {}", e.getMessage());
}
}
// 如果没有refresh_token或刷新失败需要重新获取授权
// 注意:服务端应用需要使用应用级授权,这里需要根据实际情况调整
log.warn("无法自动刷新token需要手动授权或配置应用级token");
throw new RuntimeException("无法获取访问令牌,请检查配置或手动授权");
log.warn("无法自动刷新token没有有效的refresh_token需要重新授权");
throw new RuntimeException("无法获取访问令牌,请先完成授权(访问 /jarvis/tendoc/authUrl 获取授权URL");
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
log.error("刷新访问令牌失败", e);
throw new RuntimeException("刷新访问令牌失败: " + e.getMessage(), e);