Java飞书三方网站对接

文章目录

  • 一、token获取步骤
  • 二、打卡数据同步步骤
  • 三、飞书的posthttp请求步骤
  • 四、打卡的设置步骤
  • 总结


一、token获取步骤

AppID和AppSecret在自建企业应用里获取,通过调用飞书的token接口程序获取结果,用redis把token存起来,重复利用。

    //获取飞书access_token
    public String getAccessToken() {
        String url = "https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal";
        String app_access_token = (String) redisTemplate.opsForValue().get("app_access_token");
        if (app_access_token == null || "".equals(app_access_token)) {
            Map<String,Object> map=new HashMap();
            map.put("app_id",AppID);
            map.put("app_secret",AppSecret);
            try {
                String result = sendFeiShuHttpPost(url, JSON.toJSONString(map),null);
                JSONObject jsonObject = JSONObject.parseObject(result);
                app_access_token = jsonObject.getString("app_access_token");
                Integer expire = Integer.valueOf(jsonObject.getString("expire"));//有效时间
                redisTemplate.opsForValue().set("app_access_token", app_access_token, expire, TimeUnit.SECONDS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return app_access_token;
    }

二、打卡数据同步步骤

url拼接的时候,实在链接后面拼接官方文档说查询参数。我需要的是employee_id,所以拼接employee_id。
时间需要自己定义,官方的格式是yyyyMMdd。

代码如下(示例):

 public Object getFeiShuAttendanceAll(String data,List<String> allEmployees){
        String accessToken = getAccessToken();
        String url= "https://open.feishu.cn/open-apis/attendance/v1/user_tasks/query?employee_type=employee_id&ignore_invalid_users=true";
        Map<String,Object> map=new HashMap();
        int nowData=Integer.valueOf(data);
        map.put("user_ids",allEmployees);
        map.put("check_date_from",nowData);
        map.put("check_date_to",nowData);
        JSONObject jsonObject=null;
        try {
            String result =sendFeiShuHttpPost(url, JSON.toJSONString(map),accessToken);
            jsonObject = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject ;
    }

三、飞书的posthttp请求步骤

    //飞书http请求
    public static String sendFeiShuHttpPost(String url, String JSONBody,String token) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        if (!com.luttica.core.util.StringUtils.isEmpty(token)){
            httpPost.addHeader("Authorization","Bearer "+token);
        }
        httpPost.setEntity(new StringEntity(JSONBody));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
        response.close();
        httpClient.close();
        return responseContent;
    }

四、打卡的设置步骤

需要登录飞书后台管理在飞书自带的打卡的程序里选择账号。

最后在自建的应用程序申请读取打卡的权限。


总结

提示:这里对文章进行总结:
例如:本文仅仅简单介绍了飞书的打卡的api的使用。通过这几个步骤获取打卡数据,之后的业务代码就需要自己去具体处理。

你可能感兴趣的:(java,微信,开发语言)