|
|
|
@ -6,8 +6,10 @@ 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;
|
|
|
|
@ -33,15 +35,14 @@ public class TestDingTalk {
|
|
|
|
|
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.getString("access_token");
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
JSONObject jo = JSONObject.parseObject(str);
|
|
|
|
|
System.out.println("本轮是从缓存中获取到Token:" + jo.getString("access_token"));
|
|
|
|
|
return jo.getString("access_token");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的JsApiToken,有效期7200秒,即2小时,每小时最好重新获取一次
|
|
|
|
|
作者:黄海
|
|
|
|
@ -55,11 +56,31 @@ public class TestDingTalk {
|
|
|
|
|
req.setHttpMethod("GET");
|
|
|
|
|
OapiGetJsapiTicketResponse rsp = client.execute(req, access_token);
|
|
|
|
|
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期
|
|
|
|
|
System.out.println("本轮是从从阿里云获取到JsApiToken:" + rsp.getBody());
|
|
|
|
|
return rsp.getBody();
|
|
|
|
|
} else {
|
|
|
|
|
String str = Redis.use().get(KEY);
|
|
|
|
|
System.out.println("本轮是从缓存中获取到JsApiToken:" + str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
功能:获取钉钉的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);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -67,6 +88,7 @@ public class TestDingTalk {
|
|
|
|
|
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");
|
|
|
|
@ -79,5 +101,8 @@ public class TestDingTalk {
|
|
|
|
|
System.out.println(accessToken);
|
|
|
|
|
String jsApiToken = getJsApiToken(accessToken);
|
|
|
|
|
System.out.println(jsApiToken);
|
|
|
|
|
String SsoToken = getSsoToken(corpId, ssoSecret);
|
|
|
|
|
System.out.println(SsoToken);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|