Pu Zhibing
14 小时以前 9ce4e406f87b35fb9b707f5b4a7e0999032730f2
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
package com.ruoyi.order.util.kuaishou;
 
import com.alibaba.fastjson.JSON;
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 java.util.concurrent.TimeUnit;
 
/**
 * 快手获取client_token工具类
 * @author zhibing.pu
 * @Date 2025/6/11 18:46
 */
@Slf4j
public class KSClientTokenUtil {
    
    
    /**
     * 获取client_token
     */
    public static void getClientToken(RedisService redisService, String code) {
        try {
            OAuthAccessTokenKsClient client = new OAuthAccessTokenKsClient(KuaiShouConfig.appKey, KuaiShouConfig.appSecret);
            KsAccessTokenPreviousVersionResponse response = client.getAccessToken(code);
            log.info("快手获取client_token结果:" + JSON.toJSONString(response));
            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 (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
    
    
    /**
     * 刷新client_token
     */
    public static void refreshToken(RedisService redisService) {
        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 (Exception e) {
            e.printStackTrace();
        }
    }
}