Pu Zhibing
1 天以前 878ac7cc8e1951bbc7b27619c4e7ece1e3d331ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 ClientTokenUtil {
    
    
    /**
     * 获取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) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            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());
        }
    }
}