|
|
|
@ -0,0 +1,114 @@
|
|
|
|
|
package UnitTest;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
|
|
|
|
|
import com.aliyun.tea.TeaException;
|
|
|
|
|
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_New {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的Token,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
|
时间:2023-06-06
|
|
|
|
|
*/
|
|
|
|
|
public static String getAccessToken(String appKey, String appSecret) throws Exception {
|
|
|
|
|
final String KEY = "DingTalkAccessToken";
|
|
|
|
|
if (!Redis.use().exists(KEY)) {
|
|
|
|
|
String accessToken=null;
|
|
|
|
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
|
|
|
|
|
config.protocol = "https";
|
|
|
|
|
config.regionId = "central";
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
|
|
|
|
|
com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
|
|
|
|
|
.setAppKey(appKey)
|
|
|
|
|
.setAppSecret(appSecret);
|
|
|
|
|
try {
|
|
|
|
|
GetAccessTokenResponse res = client.getAccessToken(getAccessTokenRequest);
|
|
|
|
|
accessToken = res.getBody().getAccessToken();
|
|
|
|
|
} catch (TeaException err) {
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception _err) {
|
|
|
|
|
TeaException err = new TeaException(_err.getMessage(), _err);
|
|
|
|
|
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Redis.use().setex(KEY, 5400, accessToken);//5400:一个半小时过期
|
|
|
|
|
return accessToken;
|
|
|
|
|
} 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();
|
|
|
|
|
|
|
|
|
|
System.out.println(getAccessToken(appKey,appSecret));
|
|
|
|
|
}
|
|
|
|
|
}
|