package com.ruoyi.order.util.kuaishou;
|
|
import com.aliyun.tea.TeaException;
|
import com.kuaishou.locallife.open.api.KsLocalLifeApiException;
|
import com.kuaishou.locallife.open.api.client.oauth.OAuthAccessTokenKsClient;
|
import com.kuaishou.locallife.open.api.response.oauth.KsAccessTokenPreviousVersionResponse;
|
import com.ruoyi.common.redis.service.RedisService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import java.util.concurrent.TimeUnit;
|
|
/**
|
* 快手获取client_token工具类
|
* @author zhibing.pu
|
* @Date 2025/6/11 18:46
|
*/
|
@Slf4j
|
@Component
|
public class KSClientTokenUtil {
|
|
|
/**
|
* 获取client_token
|
*/
|
public static void getClientToken(RedisService redisService, String code) {
|
try {
|
OAuthAccessTokenKsClient client = new OAuthAccessTokenKsClient(KuaiShouConfig.appKey, KuaiShouConfig.appSecret);
|
try {
|
KsAccessTokenPreviousVersionResponse response = client.getAccessToken(code);
|
String token = response.getAccessToken();
|
Long expiration_time = response.getExpiresIn();
|
String refreshToken = response.getRefreshToken();
|
Long refreshTokenExpiresIn = response.getRefreshTokenExpiresIn();
|
redisService.setCacheObject("ks_access_token", token, expiration_time, TimeUnit.SECONDS);
|
redisService.setCacheObject("ks_refresh_token", refreshToken, refreshTokenExpiresIn, TimeUnit.SECONDS);
|
} catch (KsLocalLifeApiException e) {
|
throw new RuntimeException(e);
|
}
|
} catch (TeaException e) {
|
e.printStackTrace();
|
System.out.println(e.getMessage());
|
} catch (Exception e) {
|
e.printStackTrace();
|
System.out.println(e.getMessage());
|
}
|
}
|
|
|
/**
|
* 刷新client_token
|
*/
|
public static void refreshToken(RedisService redisService) {
|
try {
|
Object ks_refresh_token = redisService.getCacheObject("ks_refresh_token");
|
OAuthAccessTokenKsClient client = new OAuthAccessTokenKsClient(KuaiShouConfig.appKey, KuaiShouConfig.appSecret);
|
try {
|
KsAccessTokenPreviousVersionResponse response = client.refreshAccessToken(ks_refresh_token.toString());
|
String token = response.getAccessToken();
|
Long expiration_time = response.getExpiresIn();
|
String refreshToken = response.getRefreshToken();
|
Long refreshTokenExpiresIn = response.getRefreshTokenExpiresIn();
|
redisService.setCacheObject("ks_access_token", token, expiration_time, TimeUnit.SECONDS);
|
redisService.setCacheObject("ks_refresh_token", refreshToken, refreshTokenExpiresIn, TimeUnit.SECONDS);
|
} catch (KsLocalLifeApiException e) {
|
throw new RuntimeException(e);
|
}
|
} catch (TeaException e) {
|
System.out.println(e.getMessage());
|
} catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
}
|
}
|