第3节--钉钉第三方企业应用开发所需的常见接口,以及相关调用代码

关于钉钉版本,根据自身需求而定,笔者需求需要同步组织架构用户相关,因此选的钉钉SDK版本为

<!-- 钉钉 -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alibaba-dingtalk-service-sdk</artifactId>
    <version>2.0.0</version>
</dependency>

  • 服务商获取第三方应用授权企业的access_token

https://oapi.dingtalk.com/service/get_corp_token       suiteTicket  就是前面章节 开发者中心推送的ticket  ,auth_corpid 为对应企业的钉钉id,可通过开发者中心的  ${corpId}$ 获取

    public String getAccessToken(String corpId) {
        // 1.从缓存中获取
        String suiteId=dingTalkConfig.getSuiteId();
        RBucket<String> bucket = redissonClient.getBucket(DingTalkCacheConstants.accessToken + suiteId+":"+corpId);
        String cacheAccessToken = bucket.get();
        if (StringUtils.isNotBlank(cacheAccessToken)) {
            return cacheAccessToken;
        }
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
        req.setAuthCorpid(corpId);

        String dingTalkAccessToken = null;
        try {
            OapiServiceGetCorpTokenResponse execute = client.execute(req, dingTalkConfig.getSuiteKey(), dingTalkConfig.getSuiteSecret(), getSuiteTicket(suiteId));
            dingTalkAccessToken = execute.getAccessToken();
            //access token  过期时间
            Long expireIn = execute.getExpiresIn();
            //设置缓存  直接覆盖
            bucket.set(dingTalkAccessToken, expireIn, TimeUnit.SECONDS);

            log.info("dingTalkAccessToken:{},expireIn:{}", dingTalkAccessToken, expireIn);
        } catch (Exception err) {
            log.error(err.getMessage());
        }
        return dingTalkAccessToken;
    }

  • 根据免登码获取用户信息   
     https://oapi.dingtalk.com/topapi/v2/user/getuserinfo   官方文档有代码示例
  • 获取企业下的部门
    需要遍历调用,钉钉没有直接提供接口      https://oapi.dingtalk.com/topapi/v2/department/listsubid   顶级传1L
    public void getTopDeptList(Long parentId, String corpId, List<Long> idList) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
        OapiV2DepartmentListsubidRequest req = new OapiV2DepartmentListsubidRequest();
        req.setDeptId(parentId);
        String accessToken = getAccessToken(corpId);
        OapiV2DepartmentListsubidResponse rsp = null;
        try {
            rsp = client.execute(req, accessToken);
            if (0 == rsp.getErrcode()) {
                List<Long> deptIdList = rsp.getResult().getDeptIdList();
                //递归获取所有的部门id
                for (Long id : deptIdList) {
                    idList.add(id);
                    getTopDeptList(id, corpId, idList);
                }
            } else {
                throw new BizDingTalkRuntimeException(DingTalkErrorCode.GET_DEPTS_ERROR);
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }

    }
  • 获取部门下的员工

     https://oapi.dingtalk.com/topapi/v2/user/list

 public List<OapiV2UserListResponse.ListUserResponse> getUserListByDeptId( Long deptId, Long cursor, Long size,String corpId) {
        try {
            String accessToken = getAccessToken(corpId);
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
            OapiV2UserListRequest req = new OapiV2UserListRequest();
            req.setDeptId(deptId);
            req.setCursor(cursor);
            req.setSize(size);
            OapiV2UserListResponse rsp = client.execute(req, accessToken);
            if (0!=rsp.getErrcode()){
                return new ArrayList<>();
            }
            List<OapiV2UserListResponse.ListUserResponse> list = rsp.getResult().getList();

            if (rsp.getResult().getHasMore()) {
                list.addAll(getUserListByDeptId(deptId, cursor++, size,corpId));
            }
            return list;
        } catch (ApiException e) {
            e.printStackTrace();
            throw new BizDingTalkRuntimeException(DingTalkErrorCode.GET_DEPTS_STAFF_ERROR);
        }
    }

 其他接口:按需自己写,开发者中心申请应用接口权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值