钉钉api调用

钉钉api调用

开发须知()官方

(1)调用钉钉服务端接口时,需使用HTTPS协议、JSON数据格式、UTF-8编码,POST请求请在HTTP Header中设置 Content-Type:application/json。
访问域名为:
新版服务端接口:https://api.dingtalk.com。
旧版服务端接口:https://oapi.dingtalk.com。
说明 旧版服务端接口支持正常调用。
(2)在调用服务端接口前,确保你已了解调用频率限制。详情请参考调用频率限制。
(3)在调用服务端接口前,确保你已经设置了对应的接口权限。详情请参考添加接口调用权限。
(4)无论是哪种应用,都必须接入钉钉免登,即在用户打开应用时可直接获取用户身份无需输入钉钉账号和密码。

access_token

access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token。
开发者需要缓存access_token,用于后续接口的调用。因为每个应用的access_token是彼此独立的,所以进行缓存时需要区分应用来进行存储。
不能频繁调用gettoken接口,否则会受到频率拦截。

package DingDingApiDemo;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;

import java.util.*;

/**
 * DingTalkApi工具类
 *
 * @author CaoPengCheng
 * @version 1.0.0
 * @Project DingTalkDemo
 * @Date 2021-08-31
 */
public class DingApiUntil {
    //应用的唯一标识key。
    private String appkey = "ding8lrom1le5zopavwt";
    //应用的密钥。
    private String appsecret = "T_oZq1IIEj5D5rses0EGc5gyI0EL_jqL8cLGhdFyWcdvpm8eSEwfLNEsJae0ObC-";
    private DingTalkClient client;
    private String access_token;
    private final List<Long> dept_idList = new ArrayList<>();

    //无参构造,初始化access_token
    public DingApiUntil() {
        getAccess_token();
    }

    //有参构造,初始化access_token
    public DingApiUntil(String access_token) {
        this.access_token = access_token;
    }

    //双参参构造,其他应用初始化access_token
    public DingApiUntil(String appkey, String appsecret) {
        this.appkey = appkey;
        this.appsecret = appsecret;
    }

    /*
     * 获取access_token
     * request:get
     * return:String
     * */
    public String getAccess_token() {
        OapiGettokenResponse response;
        OapiGettokenRequest request;
        String token = null;
        try {
            client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            request = new OapiGettokenRequest();
            request.setAppkey(appkey);
            request.setAppsecret(appsecret);
            request.setHttpMethod("GET");
            response = client.execute(request);
            //将response转为json,取出access_token
            token = JSONObject.parseObject(response.getBody()).getString("access_token");
            //每次获取都应该刷新access_token
            this.access_token = token;
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return token;
    }

    /*
     * 获取所有小部门Id
     * request:post
     * return:void
     * */
    private void getDept_id(Long deptId) {
        OapiV2DepartmentListsubidRequest req;
        OapiV2DepartmentListsubidResponse rsp;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsubid");
                req = new OapiV2DepartmentListsubidRequest();
                req.setDeptId(deptId);
                rsp = client.execute(req, access_token);
                String json = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("dept_id_list");
                json = json.replace("]", "");
                json = json.replace("[", "");
                if (json.equals("")) {
                    dept_idList.add(Long.valueOf(deptId));
                } else {
                    for (String str : json.split(",")) {
                        getDept_id(Long.valueOf(str));
                    }
                }
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    /*
     * 根据dept_id获取全部userId
     * request:post
     * return:
     * */
    public String[] getUserIdAllByDept_id(Long dept_id) {
        OapiUserListidRequest req;
        OapiUserListidResponse rsp;
        String json = null;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
                req = new OapiUserListidRequest();
                req.setDeptId(dept_id);
                rsp = client.execute(req, access_token);
                json = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("userid_list");
                json = json.replace("]", "");
                json = json.replace("[", "");
                json = json.replace("\"", "");
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return json.split(",");
    }

    /*
     * 根据userId获取全部信息
     * request:post
     * return:UserDetail
     * */
    public UserDetail getMassageByUserId(String userId) {
        OapiV2UserGetRequest req;
        OapiV2UserGetResponse rsp;
        UserDetail user = null;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
                req = new OapiV2UserGetRequest();
                req.setUserid(userId);
                req.setLanguage("zh_CN");
                rsp = client.execute(req, access_token);
                JSONObject result = JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result"));
                if (result != null) {
                    user = new UserDetail();
                    user.setCode(result.getString("mobile"));
                    user.setName(result.getString("name"));
                    user.setPhone(result.getString("mobile"));
                    user.setDingUserId(userId);
                    user.setPassWord("123456");
                }
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return user;
    }

    /*
     * 通过部门获取所有员工信息
     * return:List<UserDetail>
     * */
    public List<UserDetail> getUserMassgAllByDept(Set<String> oldUserid) {
        List<UserDetail> userList = null;
        getDept_id(1L);
        if (!dept_idList.isEmpty()) {
            Set<String> setUserId = new TreeSet<>();
            for (Long deptid : dept_idList) {
                String[] userid = getUserIdAllByDept_id(deptid);
                Collections.addAll(setUserId, userid);//自去重
            }
            //oldUserid非空去重
            if (!oldUserid.isEmpty()) {
                Set<String> UserIdAll = new TreeSet<>();
                //与系统中的userid去重
                setFor:
                for (String id : setUserId) {
                    for (String old : oldUserid) {
                        if (id.equals(old)) {
                            continue setFor;
                        }
                    }
                    UserIdAll.add(id);
                }
                setUserId = UserIdAll;
            }
            //setUserId非空,取信息
            if (!setUserId.isEmpty()) {
                userList = new ArrayList<>();
                for (String str : setUserId) {
                    userList.add(getMassageByUserId(str));
                }
            }
        }
        return userList;
    }

    /*
     * 电话获取UserId
     * request:post
     * return:List<UserDetail>
     * */
    public String getUserIdByTelephone(String phone) {
        OapiV2UserGetbymobileRequest req;
        OapiV2UserGetbymobileResponse rsp;
        String userId = null;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
                req = new OapiV2UserGetbymobileRequest();
                req.setMobile(phone);
                rsp = client.execute(req, access_token);
                JSONObject result = JSONObject.parseObject(rsp.getBody());
                if(result.getString("errmsg").equals("ok")){
                    userId=JSONObject.parseObject(result.getString("result")).getString("userid");
                }
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return userId;
    }

    /*
     * 电话获取当员工信息
     * return: UserDetail
     * */
    public UserDetail getUserMassgByTelephone(String phone) {
        String id = getUserIdByTelephone(phone);
        if (!id.equals("")) {
            return getMassageByUserId(id);
        }
        else {
            return null;
        }
    }

    /*
     * 获取员工人数
     * request:post
     * return:int
     * */
    public int getPersonnelCount() {
        OapiUserCountRequest req;
        OapiUserCountResponse rsp;
        int count = 0;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/count");
                req = new OapiUserCountRequest();
                req.setOnlyActive(false);
                rsp = client.execute(req, access_token);
                //将response转为json,取出access_token
                count = Integer.parseInt(JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")).getString("count"));
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return count;

    }

    //打印list
    public void printList() {
        dept_idList.forEach(System.out::println);
    }

    //刷新Access_token
    public void refreshAccess_token() {
        this.access_token = getAccess_token();
    }

    //TODO:没有授权,获取部到部门信息
    /*
     * 获取所有部门信息
     * request:post
     * return:void
     * */
    public void getDepartment() {
        OapiV2DepartmentListsubRequest req;
        OapiV2DepartmentListsubResponse rsp;
        try {
            if (access_token != null) {
                client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
                req = new OapiV2DepartmentListsubRequest();
                req.setDeptId(1L);
                req.setLanguage("zh_CN");
                rsp = client.execute(req, access_token);
                System.out.println(rsp.getBody());
                System.out.println(JSONObject.parseObject(JSONObject.parseObject(rsp.getBody()).getString("result")));
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

    public void getDepartment2() {
        try {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
            OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
            req.setDeptId(1L);
            req.setLanguage("zh_CN");
            OapiV2DepartmentGetResponse rsp;
            rsp = client.execute(req, access_token);
            System.out.println(rsp.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}

package DingDingApiDemo;

import org.junit.Test;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * DingTalkApi测试类
 *
 * @author CaoPengCheng
 * @version 1.0.0
 * @project DingTalkDemo
 * @date 2021-08-31
 */
public class DingApiTest {
    DingApiUntil dingApiUntil = new DingApiUntil();

    @Test
    public void countTest() {
        System.out.println("公司人数=" + dingApiUntil.getPersonnelCount());
    }

    @Test
    public void selectMassage() {
        UserDetail user = dingApiUntil.getMassageByUserId("manager8145");
        if (user == null) {
            System.out.println("user is null!");
        } else {
            System.out.println(user);
        }
    }

    @Test
    public void getUserIdAllByDept_idTest() {
        dingApiUntil.getUserIdAllByDept_id(537768596L);
    }

    @Test
    public void selectUserAll() {
        Set<String> newId = new TreeSet<>();
        Set<String> oldId = new TreeSet<>();
        newId.add("250214042135263180");
        newId.add("141434602621593606");
        newId.add("2502254505676061");
        newId.add("300156295239381872");
        newId.add("595749044939157569");
        newId.add("490055366940074226");
        newId.add("594261420524905377");
        newId.add("595851032426476806");
        newId.add("221639481420359250");
        newId.add("131854291335519272");
        newId.add("311633010520063562");
        newId.add("251155622521627291");
        newId.add("1648535929690701");
        newId.add("15864876864482337");
        newId.add("1585648369415541");
        oldId.add("250214042135263180");
        oldId.add("141434602621593606");
        oldId.add("2502254505676061");
        oldId.add("300156295239381872");
        oldId.add("595749044939157569");
        oldId.add("490055366940074226");
        test(oldId, newId);
    }

    public void test(Set<String> oldUserid, Set<String> setUserId) {
        Set<String> UserIdAll = new TreeSet<>();
        //与系统中的userid去重
        setFor:
        for (String id : setUserId) {

            for (String old : oldUserid) {
                if (id.equals(old)) {
                    continue setFor;
                }
            }
            UserIdAll.add(id);
        }
        for (String s : UserIdAll)
            System.out.println(s);
    }

    //通过电话精确获取
    @Test
    public void getUserIdByTelephoneTest() {
        UserDetail user=dingApiUntil.getUserMassgByTelephone("18993472079");
        if(user==null)
            System.out.println("查无此人");
        else
            System.out.println(user);
    }


    //通过部门全部获取
    @Test
    public void getuser() {
        Set<String> oldId = new TreeSet<>();
        oldId.add("manager8145");
        oldId.add("01326153421626553659");
        oldId.add("010937525215978977");
        oldId.add("manager81450");
        List<UserDetail> list = dingApiUntil.getUserMassgAllByDept(oldId);
        if (list == null)
            System.out.println("error");
        else
            list.forEach(System.out::println);
    }
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
VFP(Visual FoxPro)是一种基于对象的编程语言,常用于Windows平台上的数据库应用程序开发。要调用API接口,可以按照以下步骤进行: 1. 首先,需要在开放平台上创建一个企业应用,获取到应用的CorpID和CorpSecret。这些凭证将用于在API调用中进行身份验证。 2. 在VFP中,可以使用URLMON库来发送HTTP请求。使用URLMON库的URLDownloadToFile函数可以下载API返回的数据到本地文件。同时,还可以使用API相关的地址和参数构建需要调用的URL。 3. 在VFP中,可以使用ADO(ActiveX Data Objects)来处理HTTP请求的返回值。通过创建一个ADODB.Stream对象,可以读取下载的API返回的数据,并进行进一步的操作和处理。 4. 在进行API调用时,需要对请求进行签名验证,以确保请求的合法性和安全性。可以使用HMAC-SHA256算法对请求参数进行签名,将签名结果添加到URL中的请求参数中,以验证请求的有效性。 5. 在VFP中,可以使用API的请求参数的JSON格式来进行请求。可以使用VFP的JSON类库或者其他JSON解析器来处理JSON格式的请求参数和返回值。 在调用API接口时,需要仔细阅读开放平台的API文档,了解每个接口的具体使用方法和请求参数,以确保API调用的成功和准确性。完成以上步骤后,即可在VFP中调用API接口,实现与的数据交互和业务操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CaoPengCheng&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值