main
黄海 2 years ago
parent 7d5f00858b
commit 86dd34f3b1

@ -2,7 +2,10 @@ package UnitTest;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenResponse;
import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
@ -18,6 +21,18 @@ import com.jfinal.plugin.redis.RedisPlugin;
import com.taobao.api.ApiException;
public class TestDingTalk_New {
/**
* 使 Token Client
*
* @return Client
* @throws Exception
*/
public static com.aliyun.dingtalkoauth2_1_0.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
config.protocol = "https";
config.regionId = "central";
return new com.aliyun.dingtalkoauth2_1_0.Client(config);
}
/*
Token,72002
@ -27,11 +42,8 @@ public class TestDingTalk_New {
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);
String accessToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
.setAppKey(appKey)
.setAppSecret(appSecret);
@ -54,20 +66,33 @@ public class TestDingTalk_New {
return jo.getString("access_token");
}
}
/*
JsApiToken,72002
2023-06-06
*/
public static String getJsApiToken(String access_token) throws ApiException {
public static String getJsApiToken(String access_token) throws Exception {
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();
String jsApiToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders createJsapiTicketHeaders = new com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders();
createJsapiTicketHeaders.xAcsDingtalkAccessToken = access_token;
try {
CreateJsapiTicketResponse res = client.createJsapiTicketWithOptions(createJsapiTicketHeaders, new com.aliyun.teautil.models.RuntimeOptions());
jsApiToken = res.getBody().getJsapiTicket();
} 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, jsApiToken);//5400:一个半小时过期
return jsApiToken;
} else {
String str = Redis.use().get(KEY);
JSONObject jo = JSONObject.parseObject(str);
@ -80,23 +105,34 @@ public class TestDingTalk_New {
2023-06-06
*/
public static String getSsoToken(String corpId, String ssoSecret) throws ApiException {
public static String getSsoToken(String corpId, String ssoSecret) throws Exception {
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();
String ssoToken = null;
com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
GetSsoAccessTokenRequest getSsoAccessTokenRequest = new GetSsoAccessTokenRequest()
.setCorpid(corpId)
.setSsoSecret(ssoSecret);
try {
GetSsoAccessTokenResponse res=client.getSsoAccessToken(getSsoAccessTokenRequest);
ssoToken=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, ssoToken);//5400:一个半小时过期
return ssoToken;
} 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");
@ -109,6 +145,13 @@ public class TestDingTalk_New {
RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
redis.start();
System.out.println(getAccessToken(appKey,appSecret));
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);
}
}

Loading…
Cancel
Save