钉钉接口:获取accessToken和打卡记录

post和get方法工具类:HttpUtils

package weaver.dingtalk.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import weaver.interfaces.email.SSLClient;

import java.util.ArrayList;
import java.util.List;

/**
 * http请求工具类
 * @author lcs
 */
public class HttpUtils {

    public static String doPost(String url, JSONObject jsonObject, String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            //设置参数
            List list = new ArrayList();
            StringEntity entity = new StringEntity(jsonObject.toString(),charset);
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            HttpResponse response = httpClient.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }

    public static String doGet(String url){
        String result = null;
        HttpGet request = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpResponse response = httpClient.execute(request);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result= EntityUtils.toString(response.getEntity(),"utf-8");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}

钉钉接口方法:DingTalkService

package weaver.dingtalk.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.dingtalk.utils.HttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * DingTalk相关方法
 * @author 刘春生
 */
public class DingTalkService {

    /**
     * 获取钉钉的accessToken
     * @param url
     * @param corpid
     * @param secret
     * @return
     */
    public static String getAccessToken(String url,String corpid,String secret){
        String requestUrl = url + "?corpid="+corpid+"&corpsecret="+secret;
        String result = HttpUtils.doGet(requestUrl);
        String accessToken = null;
        JSONObject jsonObject = new JSONObject();
        jsonObject = JSON.parseObject(result);
        String msg = (String)jsonObject.get("errmsg");
        if("ok".equals(msg)){
            accessToken = (String)jsonObject.get("access_token");
        }
        return accessToken;
    }

    public static JSONArray getCardList(String accessToken, String workDateFrom, String workDateTo, String offset, String limit){
        String recordUrl = "https://oapi.dingtalk.com/attendance/list?access_token="+accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("workDateFrom",workDateFrom);
        jsonObject.put("workDateTo",workDateTo);
        jsonObject.put("offset",offset);
        jsonObject.put("limit",limit);
        String result = HttpUtils.doPost(recordUrl,jsonObject,"utf-8");
        JSONObject resutJSON = JSONObject.parseObject(result);
        String msg = (String)resutJSON.get("errmsg");
        JSONArray jsonArray = null;
        if("ok".equals(msg)){
            jsonArray = (JSONArray) resutJSON.get("recordresult");
        }
        return jsonArray;
    }
}

测试方法:TestDingTalk

package weaver.dingtalk.service;

import com.alibaba.fastjson.JSONArray;

public class TestDingTalk {
    public static void main(String[] args) {
        String workDateFrom = "2017-11-03 08:00:00";
        String workDateTo = "2017-11-03 18:00:00";
        String offset = "0";//分页获取数据,0表示第一页
        String limit = "10";//每页10条数据
        String accessTokenUrl = "https://oapi.dingtalk.com/gettoken";
        String corpid = "##############";//替换成自己的corpid
        String secret = "##############";//替换成自己的corpsecret
        String accessToken = DingTalkService.getAccessToken(accessTokenUrl,corpid,secret);
        if(accessToken!=null){
            JSONArray jsonArray = DingTalkService.getCardList(accessToken,workDateFrom,workDateTo,offset,limit);
            System.out.println(jsonArray);
        }
    }
}

你可能感兴趣的:(java)