|
|
|
@ -1,111 +0,0 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
|
|
import com.dingtalk.api.request.OapiGetJsapiTicketRequest;
|
|
|
|
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
|
|
|
|
import com.dingtalk.api.request.OapiSsoGettokenRequest;
|
|
|
|
|
import com.dingtalk.api.response.OapiGetJsapiTicketResponse;
|
|
|
|
|
import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
|
|
import com.dingtalk.api.response.OapiSsoGettokenResponse;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
|
import com.jfinal.plugin.redis.Redis;
|
|
|
|
|
import com.jfinal.plugin.redis.RedisPlugin;
|
|
|
|
|
import com.taobao.api.ApiException;
|
|
|
|
|
|
|
|
|
|
public class TestDingTalk_Old {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的Token,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String 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());
|
|
|
|
|
Redis.use().setex(KEY, 5400, jo.toString());//5400:一个半小时过期
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的JsApiToken,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getJsApiToken(String access_token) throws ApiException {
|
|
|
|
|
final String KEY = "DingTalkJsApiToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/get_jsapi_ticket");
|
|
|
|
|
OapiGetJsapiTicketRequest req = new OapiGetJsapiTicketRequest();
|
|
|
|
|
req.setHttpMethod("GET");
|
|
|
|
|
OapiGetJsapiTicketResponse rsp = client.execute(req, access_token);
|
|
|
|
|
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期
|
|
|
|
|
return rsp.getBody();
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
return jo.getString("ticket");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的SsoToken,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getSsoToken(String corpId, String ssoSecret) throws ApiException {
|
|
|
|
|
final String KEY = "DingTalkSsoToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sso/gettoken");
|
|
|
|
|
OapiSsoGettokenRequest req = new OapiSsoGettokenRequest();
|
|
|
|
|
req.setCorpid(corpId);
|
|
|
|
|
req.setCorpsecret(ssoSecret);
|
|
|
|
|
req.setHttpMethod("GET");
|
|
|
|
|
OapiSsoGettokenResponse rsp = client.execute(req);
|
|
|
|
|
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期
|
|
|
|
|
return rsp.getBody();
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args_) throws Exception {
|
|
|
|
|
PropKit.use("dingtalk.properties");
|
|
|
|
|
String corpId = PropKit.get("corpId");
|
|
|
|
|
String ssoSecret = PropKit.get("SSOSecret");
|
|
|
|
|
long agentId = PropKit.getLong("agentId");
|
|
|
|
|
final String appKey = PropKit.get("appKey");
|
|
|
|
|
String appSecret = PropKit.get("appSecret");
|
|
|
|
|
|
|
|
|
|
// 用于缓存模块的redis服务
|
|
|
|
|
RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
String accessToken = getAccessToken(appKey, appSecret);
|
|
|
|
|
System.out.println("accessToken=" + accessToken);
|
|
|
|
|
|
|
|
|
|
String jsApiToken = getJsApiToken(accessToken);
|
|
|
|
|
System.out.println("ticket=" + jsApiToken);
|
|
|
|
|
|
|
|
|
|
String SsoToken = getSsoToken(corpId, ssoSecret);
|
|
|
|
|
System.out.println("ssoToken=" + SsoToken);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|