|
|
|
@ -1,12 +1,20 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
|
|
|
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.redis.Redis;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
public class TestDingTalk {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -15,14 +23,26 @@ public class TestDingTalk {
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getToken(String appKey, String appSecret) throws ApiException {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
|
|
OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
|
|
request.setAppkey(appKey);
|
|
|
|
|
request.setAppsecret(appSecret);
|
|
|
|
|
request.setHttpMethod("GET");
|
|
|
|
|
OapiGettokenResponse response = client.execute(request);
|
|
|
|
|
return response.getBody();
|
|
|
|
|
public static JSONObject getAccessToken(String appKey, String appSecret) throws ApiException {
|
|
|
|
|
final String KEY = "DingTalkAccessToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
|
|
OapiGettokenRequest request = new OapiGettokenRequest();
|
|
|
|
|
request.setAppkey(appKey);
|
|
|
|
|
request.setAppsecret(appSecret);
|
|
|
|
|
request.setHttpMethod("GET");
|
|
|
|
|
OapiGettokenResponse response = client.execute(request);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(response.getBody());
|
|
|
|
|
jo.put("start_time", DateTime.now());
|
|
|
|
|
Redis.use().setex(KEY, 5400, jo.toString());//5400:一个半小时过期
|
|
|
|
|
System.out.println("本轮是从从阿里云获取到Token:" + jo.getString("access_token"));
|
|
|
|
|
return jo;
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
System.out.println("本轮是从缓存中获取到Token:" + jo.getString("access_token"));
|
|
|
|
|
return jo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args_) throws Exception {
|
|
|
|
@ -32,6 +52,13 @@ public class TestDingTalk {
|
|
|
|
|
final String appKey = PropKit.get("appKey");
|
|
|
|
|
String appSecret = PropKit.get("appSecret");
|
|
|
|
|
|
|
|
|
|
System.out.println(getToken(appKey, appSecret));
|
|
|
|
|
// 用于缓存模块的redis服务
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= 100; i++) {
|
|
|
|
|
JSONObject jo = getAccessToken(appKey, appSecret);
|
|
|
|
|
System.out.println(jo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|