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

@ -2,7 +2,10 @@ package UnitTest;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject; 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.GetAccessTokenResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetSsoAccessTokenResponse;
import com.aliyun.tea.TeaException; import com.aliyun.tea.TeaException;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
@ -18,6 +21,18 @@ import com.jfinal.plugin.redis.RedisPlugin;
import com.taobao.api.ApiException; import com.taobao.api.ApiException;
public class TestDingTalk_New { 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 Token,72002
@ -27,11 +42,8 @@ public class TestDingTalk_New {
public static String getAccessToken(String appKey, String appSecret) throws Exception { public static String getAccessToken(String appKey, String appSecret) throws Exception {
final String KEY = "DingTalkAccessToken"; final String KEY = "DingTalkAccessToken";
if (!Redis.use().exists(KEY)) { if (!Redis.use().exists(KEY)) {
String accessToken=null; String accessToken = null;
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config(); com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
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() com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
.setAppKey(appKey) .setAppKey(appKey)
.setAppSecret(appSecret); .setAppSecret(appSecret);
@ -54,20 +66,33 @@ public class TestDingTalk_New {
return jo.getString("access_token"); return jo.getString("access_token");
} }
} }
/* /*
JsApiToken,72002 JsApiToken,72002
2023-06-06 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"; final String KEY = "DingTalkJsApiToken";
if (!Redis.use().exists(KEY)) { if (!Redis.use().exists(KEY)) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/get_jsapi_ticket"); String jsApiToken = null;
OapiGetJsapiTicketRequest req = new OapiGetJsapiTicketRequest(); com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
req.setHttpMethod("GET"); com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders createJsapiTicketHeaders = new com.aliyun.dingtalkoauth2_1_0.models.CreateJsapiTicketHeaders();
OapiGetJsapiTicketResponse rsp = client.execute(req, access_token); createJsapiTicketHeaders.xAcsDingtalkAccessToken = access_token;
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期 try {
return rsp.getBody(); 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 { } else {
String str = Redis.use().get(KEY); String str = Redis.use().get(KEY);
JSONObject jo = JSONObject.parseObject(str); JSONObject jo = JSONObject.parseObject(str);
@ -80,23 +105,34 @@ public class TestDingTalk_New {
2023-06-06 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"; final String KEY = "DingTalkSsoToken";
if (!Redis.use().exists(KEY)) { if (!Redis.use().exists(KEY)) {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/sso/gettoken"); String ssoToken = null;
OapiSsoGettokenRequest req = new OapiSsoGettokenRequest(); com.aliyun.dingtalkoauth2_1_0.Client client = createClient();
req.setCorpid(corpId); GetSsoAccessTokenRequest getSsoAccessTokenRequest = new GetSsoAccessTokenRequest()
req.setCorpsecret(ssoSecret); .setCorpid(corpId)
req.setHttpMethod("GET"); .setSsoSecret(ssoSecret);
OapiSsoGettokenResponse rsp = client.execute(req); try {
Redis.use().setex(KEY, 5400, rsp.getBody());//5400:一个半小时过期 GetSsoAccessTokenResponse res=client.getSsoAccessToken(getSsoAccessTokenRequest);
return rsp.getBody(); 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 { } else {
String str = Redis.use().get(KEY); String str = Redis.use().get(KEY);
JSONObject jo = JSONObject.parseObject(str); JSONObject jo = JSONObject.parseObject(str);
return jo.getString("access_token"); return jo.getString("access_token");
} }
} }
public static void main(String[] args_) throws Exception { public static void main(String[] args_) throws Exception {
PropKit.use("dingtalk.properties"); PropKit.use("dingtalk.properties");
String corpId = PropKit.get("corpId"); 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); RedisPlugin redis = new RedisPlugin("myRedis", PropKit.get("redis_ip"), PropKit.getInt("redis_port"), 10 * 1000);
redis.start(); 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