123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- package com.fjhx.utils;
- 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.tea.TeaException;
- import com.aliyun.teaopenapi.models.Config;
- import com.aliyun.teautil.models.RuntimeOptions;
- import com.dingtalk.api.DefaultDingTalkClient;
- import com.dingtalk.api.DingTalkClient;
- import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
- import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
- import com.ruoyi.common.exception.ServiceException;
- import com.taobao.api.ApiException;
- import lombok.extern.slf4j.Slf4j;
- /**
- * 钉钉第三方企业应用 - h5微应用
- *
- * <p>
- * <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/obtain-identity-credentials"> 获取登录用户的访问凭证 </a>
- * </p>
- */
- @Slf4j
- public class DingUtil {
- public static void main(String[] args) {
- GetUserTokenResponseBody userToken = DingUtil.getUserToken("8f9511a2dbf439328ee0331a3fee125c123");
- 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
- */
- public static GetUserTokenResponseBody getUserToken(String code) {
- GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
- .setClientId(SUITE_KEY)
- .setClientSecret(SUITE_SECRET)
- .setCode(code)
- .setGrantType("authorization_code");
- try {
- com.aliyun.dingtalkoauth2_1_0.Client client = getClient2();
- return client.getUserToken(getUserTokenRequest).getBody();
- } catch (Exception e) {
- TeaException teaException;
- if (e instanceof TeaException) {
- teaException = (TeaException) e;
- } else {
- teaException = new TeaException(e.getMessage(), e);
- }
- String errCode = teaException.getCode();
- String errMsg = teaException.getMessage();
- if (StrUtil.isAllNotBlank(errCode, errMsg)) {
- log.error("钉钉授权认证失败: code:{},message:{}", errCode, errMsg);
- throw new ServiceException(errMsg);
- } else {
- log.error("钉钉授权认证失败", teaException);
- throw new ServiceException("发生未知异常,钉钉授权认证失败");
- }
- }
- }
- /**
- * 获取用户信息
- *
- * <p>
- * 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);
- try {
- OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken);
- return rsp.getResult();
- } catch (ApiException e) {
- String message = "钉钉获取用户信息失败: code:" + e.getErrCode() + ",message:" + e.getErrMsg();
- log.error(message);
- throw new ServiceException(message);
- }
- }
- /**
- * 获取用户个人信息
- *
- * <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 {
- Config config = new Config();
- config.protocol = "https";
- config.regionId = "central";
- return new com.aliyun.dingtalkcontact_1_0.Client(config);
- }
- public static com.aliyun.dingtalkoauth2_1_0.Client getClient2() throws Exception {
- Config config = new Config();
- config.protocol = "https";
- config.regionId = "central";
- return new com.aliyun.dingtalkoauth2_1_0.Client(config);
- }
- }
|