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

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

1
2
3
4
5
6
<!-- 钉钉 -->
<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}$ 获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
       }
   }

  

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

posted @   夏风中的Young_Uncle  阅读(474)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示