[點晴永久免費OA]釘釘接口:獲取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); } } }
該文章在 2020/4/27 18:09:14 編輯過 |
關鍵字查詢
相關文章
正在查詢... |