LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[点晴永久免费OA]钉钉接口:获取accessToken和打卡记录

admin
2020年4月27日 18:7 本文热度 3282
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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved