关于钉钉版本,根据自身需求而定,笔者需求需要同步组织架构用户相关,因此选的钉钉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);
}
}
其他接口:按需自己写,开发者中心申请应用接口权限。