DingUtil.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.fjhx.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.aliyun.dingtalkcontact_1_0.models.GetUserHeaders;
  4. import com.aliyun.dingtalkcontact_1_0.models.GetUserResponseBody;
  5. import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
  6. import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponseBody;
  7. import com.aliyun.tea.TeaException;
  8. import com.aliyun.teaopenapi.models.Config;
  9. import com.aliyun.teautil.models.RuntimeOptions;
  10. import com.dingtalk.api.DefaultDingTalkClient;
  11. import com.dingtalk.api.DingTalkClient;
  12. import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
  13. import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
  14. import com.ruoyi.common.exception.ServiceException;
  15. import com.taobao.api.ApiException;
  16. import lombok.extern.slf4j.Slf4j;
  17. /**
  18. * 钉钉第三方企业应用 - h5微应用
  19. *
  20. * <p>
  21. * <a href="https://open.dingtalk.com/document/isvapp/basic-concepts"> 基础概念 </a>
  22. * <a href="https://open.dingtalk.com/document/isvapp/api-overview"> 钉钉api总览 </a>
  23. * <a href="https://open.dingtalk.com/document/isvapp/obtain-identity-credentials"> 获取登录用户的访问凭证 </a>
  24. * </p>
  25. */
  26. @Slf4j
  27. public class DingUtil {
  28. public static void main(String[] args) {
  29. GetUserTokenResponseBody userToken = DingUtil.getUserToken("8f9511a2dbf439328ee0331a3fee125c123");
  30. String accessToken = userToken.getAccessToken();
  31. }
  32. private static final String SUITE_KEY = "suite8j0xog63udtsaq7g";
  33. private static final String SUITE_SECRET = "NdbWhD6Iu9n5h-3IKXeepmPwmcFbOw9Em7UeiJKyq3_Wa8LgmJ-G6b8SmsOJMrMc";
  34. /**
  35. * 获取用户token
  36. *
  37. * <p>
  38. * https://open.dingtalk.com/document/isvapp/obtain-user-token
  39. */
  40. public static GetUserTokenResponseBody getUserToken(String code) {
  41. GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
  42. .setClientId(SUITE_KEY)
  43. .setClientSecret(SUITE_SECRET)
  44. .setCode(code)
  45. .setGrantType("authorization_code");
  46. try {
  47. com.aliyun.dingtalkoauth2_1_0.Client client = getClient2();
  48. return client.getUserToken(getUserTokenRequest).getBody();
  49. } catch (Exception e) {
  50. TeaException teaException;
  51. if (e instanceof TeaException) {
  52. teaException = (TeaException) e;
  53. } else {
  54. teaException = new TeaException(e.getMessage(), e);
  55. }
  56. String errCode = teaException.getCode();
  57. String errMsg = teaException.getMessage();
  58. if (StrUtil.isAllNotBlank(errCode, errMsg)) {
  59. log.error("钉钉授权认证失败: code:{},message:{}", errCode, errMsg);
  60. throw new ServiceException(errMsg);
  61. } else {
  62. log.error("钉钉授权认证失败", teaException);
  63. throw new ServiceException("发生未知异常,钉钉授权认证失败");
  64. }
  65. }
  66. }
  67. /**
  68. * 获取用户信息
  69. *
  70. * <p>
  71. * https://open.dingtalk.com/document/isvapp/obtain-the-userid-of-a-user-by-using-the-log-free
  72. */
  73. public static OapiV2UserGetuserinfoResponse.UserGetByCodeResponse getUserInfo(String code, String accessToken) {
  74. DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
  75. OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
  76. req.setCode(code);
  77. try {
  78. OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken);
  79. return rsp.getResult();
  80. } catch (ApiException e) {
  81. String message = "钉钉获取用户信息失败: code:" + e.getErrCode() + ",message:" + e.getErrMsg();
  82. log.error(message);
  83. throw new ServiceException(message);
  84. }
  85. }
  86. /**
  87. * 获取用户个人信息
  88. *
  89. * <p>
  90. * https://open.dingtalk.com/document/isvapp/tutorial-enabling-login-to-third-party-websites
  91. */
  92. public GetUserResponseBody getUserinfo(String accessToken) throws Exception {
  93. com.aliyun.dingtalkcontact_1_0.Client client = getClient1();
  94. GetUserHeaders getUserHeaders = new GetUserHeaders();
  95. getUserHeaders.setXAcsDingtalkAccessToken(accessToken);
  96. // 获取用户个人信息,如需获取当前授权人的信息,unionId参数必须传me
  97. return client.getUserWithOptions("me", getUserHeaders, new RuntimeOptions()).getBody();
  98. }
  99. private static com.aliyun.dingtalkcontact_1_0.Client getClient1() throws Exception {
  100. Config config = new Config();
  101. config.protocol = "https";
  102. config.regionId = "central";
  103. return new com.aliyun.dingtalkcontact_1_0.Client(config);
  104. }
  105. public static com.aliyun.dingtalkoauth2_1_0.Client getClient2() throws Exception {
  106. Config config = new Config();
  107. config.protocol = "https";
  108. config.regionId = "central";
  109. return new com.aliyun.dingtalkoauth2_1_0.Client(config);
  110. }
  111. }