|
@@ -1,17 +1,16 @@
|
|
package com.fjhx.utils;
|
|
package com.fjhx.utils;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
-import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
|
|
|
|
-import com.aliyun.dingtalkcontact_1_0.models.GetUserResponseBody;
|
|
|
|
-import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
|
|
|
|
-import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponseBody;
|
|
|
|
|
|
+import com.aliyun.dingtalkoauth2_1_0.Client;
|
|
|
|
+import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenRequest;
|
|
|
|
+import com.aliyun.dingtalkoauth2_1_0.models.GetCorpAccessTokenResponseBody;
|
|
import com.aliyun.tea.TeaException;
|
|
import com.aliyun.tea.TeaException;
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
-import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
import com.dingtalk.api.DingTalkClient;
|
|
import com.dingtalk.api.DingTalkClient;
|
|
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
|
|
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
|
|
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
|
|
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
|
|
|
|
+import com.fjhx.constant.Constant;
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
import com.taobao.api.ApiException;
|
|
import com.taobao.api.ApiException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -23,37 +22,29 @@ import lombok.extern.slf4j.Slf4j;
|
|
* <p>
|
|
* <p>
|
|
* <a href="https://open.dingtalk.com/document/isvapp/basic-concepts"> 基础概念 </a>
|
|
* <a href="https://open.dingtalk.com/document/isvapp/basic-concepts"> 基础概念 </a>
|
|
* <a href="https://open.dingtalk.com/document/isvapp/api-overview"> 钉钉api总览 </a>
|
|
* <a href="https://open.dingtalk.com/document/isvapp/api-overview"> 钉钉api总览 </a>
|
|
- * <a href="https://open.dingtalk.com/document/isvapp/obtain-identity-credentials"> 获取登录用户的访问凭证 </a>
|
|
|
|
|
|
+ * <a href="https://open.dingtalk.com/document/isvapp/third-party-enterprise-application-logon-free"> 免登流程 </a>
|
|
* </p>
|
|
* </p>
|
|
*/
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
public class DingUtil {
|
|
public class DingUtil {
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
|
- GetUserTokenResponseBody userToken = DingUtil.getUserToken("9688f43aebe531238c9398fa5004c7a7");
|
|
|
|
- String accessToken = userToken.getAccessToken();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static final String SUITE_KEY = "suite8j0xog63udtsaq7g";
|
|
|
|
- private static final String SUITE_SECRET = "NdbWhD6Iu9n5h-3IKXeepmPwmcFbOw9Em7UeiJKyq3_Wa8LgmJ-G6b8SmsOJMrMc";
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
- * 获取用户token
|
|
|
|
- *
|
|
|
|
- * <p>
|
|
|
|
- * https://open.dingtalk.com/document/isvapp/obtain-user-token
|
|
|
|
|
|
+ * 获取第三方应用授权企业的accessToken
|
|
|
|
+ * https://open.dingtalk.com/document/isvapp/obtain-the-access_token-of-the-authorized-enterprise
|
|
*/
|
|
*/
|
|
- public static GetUserTokenResponseBody getUserToken(String code) {
|
|
|
|
- GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
|
|
|
|
- .setClientId(SUITE_KEY)
|
|
|
|
- .setClientSecret(SUITE_SECRET)
|
|
|
|
- .setCode(code)
|
|
|
|
- .setGrantType("authorization_code");
|
|
|
|
|
|
+ public static GetCorpAccessTokenResponseBody getCorpAccessToken(String corpId) {
|
|
|
|
+
|
|
|
|
+ GetCorpAccessTokenRequest getCorpAccessTokenRequest = new GetCorpAccessTokenRequest()
|
|
|
|
+ .setSuiteKey(Constant.SUITE_KEY)
|
|
|
|
+ .setSuiteSecret(Constant.SUITE_SECRET)
|
|
|
|
+ .setAuthCorpId(corpId)
|
|
|
|
+ .setSuiteTicket("Kn3hJvJR34e8rUEob0W7nqxK2YHqobAyPsuIQXYR35SsJMp6rE5XQBz9nwXTrJRZaVmXc4z9KjYkzXdkUdS8O3");
|
|
|
|
|
|
try {
|
|
try {
|
|
- com.aliyun.dingtalkoauth2_1_0.Client client = getClient2();
|
|
|
|
- return client.getUserToken(getUserTokenRequest).getBody();
|
|
|
|
|
|
+ Client client = getClient2();
|
|
|
|
+ GetCorpAccessTokenResponseBody corpAccessToken = client.getCorpAccessToken(getCorpAccessTokenRequest).getBody();
|
|
|
|
+ return corpAccessToken;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
|
|
|
TeaException teaException;
|
|
TeaException teaException;
|
|
@@ -74,44 +65,27 @@ public class DingUtil {
|
|
log.error("钉钉授权认证失败", teaException);
|
|
log.error("钉钉授权认证失败", teaException);
|
|
throw new ServiceException("发生未知异常,钉钉授权认证失败");
|
|
throw new ServiceException("发生未知异常,钉钉授权认证失败");
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取用户信息
|
|
|
|
- *
|
|
|
|
- * <p>
|
|
|
|
|
|
+ * 通过免登码获取用户信息
|
|
* https://open.dingtalk.com/document/isvapp/obtain-the-userid-of-a-user-by-using-the-log-free
|
|
* https://open.dingtalk.com/document/isvapp/obtain-the-userid-of-a-user-by-using-the-log-free
|
|
*/
|
|
*/
|
|
- public static OapiV2UserGetuserinfoResponse.UserGetByCodeResponse getUserInfo(String code, String accessToken) {
|
|
|
|
- DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
|
|
|
|
- OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
|
|
|
|
- req.setCode(code);
|
|
|
|
-
|
|
|
|
|
|
+ public static OapiV2UserGetuserinfoResponse.UserGetByCodeResponse getUserInfo(String corpAccessToken, String code) {
|
|
try {
|
|
try {
|
|
- OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken);
|
|
|
|
|
|
+ DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
|
|
|
|
+ OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
|
|
|
|
+ req.setCode(code);
|
|
|
|
+ OapiV2UserGetuserinfoResponse rsp = client.execute(req, corpAccessToken);
|
|
return rsp.getResult();
|
|
return rsp.getResult();
|
|
} catch (ApiException e) {
|
|
} catch (ApiException e) {
|
|
- String message = "钉钉获取用户信息失败: code:" + e.getErrCode() + ",message:" + e.getErrMsg();
|
|
|
|
- log.error(message);
|
|
|
|
- throw new ServiceException(message);
|
|
|
|
|
|
+ log.error("通过免登码获取用户信息失败");
|
|
|
|
+ throw new ServiceException("通过免登码获取用户信息失败");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取用户个人信息
|
|
|
|
- *
|
|
|
|
- * <p>
|
|
|
|
- * https://open.dingtalk.com/document/isvapp/tutorial-enabling-login-to-third-party-websites
|
|
|
|
- */
|
|
|
|
- public GetUserResponseBody getUserinfo(String accessToken) throws Exception {
|
|
|
|
- com.aliyun.dingtalkcontact_1_0.Client client = getClient1();
|
|
|
|
- GetUserHeaders getUserHeaders = new GetUserHeaders();
|
|
|
|
- getUserHeaders.setXAcsDingtalkAccessToken(accessToken);
|
|
|
|
-
|
|
|
|
- // 获取用户个人信息,如需获取当前授权人的信息,unionId参数必须传me
|
|
|
|
- return client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody();
|
|
|
|
- }
|
|
|
|
|
|
|
|
private static com.aliyun.dingtalkcontact_1_0.Client getClient1() throws Exception {
|
|
private static com.aliyun.dingtalkcontact_1_0.Client getClient1() throws Exception {
|
|
Config config = new Config();
|
|
Config config = new Config();
|
|
@@ -120,7 +94,7 @@ public class DingUtil {
|
|
return new com.aliyun.dingtalkcontact_1_0.Client(config);
|
|
return new com.aliyun.dingtalkcontact_1_0.Client(config);
|
|
}
|
|
}
|
|
|
|
|
|
- public static com.aliyun.dingtalkoauth2_1_0.Client getClient2() throws Exception {
|
|
|
|
|
|
+ private static com.aliyun.dingtalkoauth2_1_0.Client getClient2() throws Exception {
|
|
Config config = new Config();
|
|
Config config = new Config();
|
|
config.protocol = "https";
|
|
config.protocol = "https";
|
|
config.regionId = "central";
|
|
config.regionId = "central";
|