公司需要,搞了一下钉钉的接口。

首先说明我是帮使用钉钉软件的客户通过钉钉的接口获取数据,所以说我是有权限的。如果你是ISV,去看官方文档吧。

我暂时还不知道ISV开发人员是什么需求,但是至少对于钉钉来说我觉得挺麻烦的。点击打开链接

这是钉钉的官方文档地址:点击打开链接

我搞了半天才分清一点ISV和非ISV是什么,反正大抵意思是一个授权和一个非授权的访问接口的方式。

非ISV开发人员需要获取access_token(好像是默认两小时生命周期,超过时间若在持续获取数据就会自动延续,否则过期需要重新获取。),

这个获取接口是https://oapi.dingtalk.com/gettoken?corpid=corpid&corpsecret=corpsecret,

其中的corpid和corpsecret这样获取:点击打开链接

java get方式获取示例代码:

public static String get(String url) {
		String result = "";
		BufferedReader in = null;
		try {
			URL getUrl = new URL(url);
			URLConnection connection = getUrl.openConnection();// 建立http连接
			connection.setRequestProperty("Charset", "UTF-8");
			connection.setRequestProperty("accept", "*/*");// 设置通用的请求属性
			connection.connect();// 开始连接请求
			in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));//这里如果不加编码方式,解析请求回来的json可能中文乱码报错
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常!" + e);
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return result;
	}


下面说POST方式获取数据,看一下接口文档就知道了,严格的参数要求,初次搞反正很蛋疼。

@SuppressWarnings("unchecked")
	public void post() throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		String access_tokenJson = "https://oapi.dingtalk.com/gettoken?corpid=corpid&corpsecret=corpsecret";
		JSONObject accessJson = JSONObject.fromObject(get(access_tokenJson));
		DD_access_token token= (DD_access_token)JSONObject.toBean(accessJson,DD_access_token.class);
        /*
         		钉钉取打卡记录的接口传参列表:
	         {
			    "userId": "员工在企业内的UserID,企业用来唯一标识用户的字段",//非必填
			    "workDateFrom": "yyyy-MM-dd hh:mm:ss",
			    "workDateTo": "yyyy-MM-dd hh:mm:ss"
			 }
        */
        JSONObject jsonObj = new JSONObject();
 		Date curTime = new Date();
 		//jsonObj.put("userId", "userId");//userid查询条件
        jsonObj.put("workDateFrom", sdf.format(curTime)+" 00:00:00");
 		jsonObj.put("workDateTo", sdf.format(curTime));
		String strURL = "https://oapi.dingtalk.com/attendance/list?access_token="+token.getAccess_token()+"";
		try {
			URL url = new URL(strURL);// 创建url资源
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 建立http连接
			conn.setDoOutput(true);// 设置允许输出
			conn.setDoInput(true);// 设置允许输入
			conn.setUseCaches(false);// 设置不用缓存
			conn.setRequestMethod("POST");// 设置传参方式
			conn.setRequestProperty("Charset", "UTF-8");
			conn.setRequestProperty("Content-Type", "application/json");
			conn.connect();// 开始连接请求
			OutputStream out = conn.getOutputStream();
			out.write((jsonObj.toString()).getBytes());// 写入请求的字符串
			out.flush();
			out.close();
			if (conn.getResponseCode() == 200) {// 请求返回的状态
				// 请求返回的数据
				InputStream in = conn.getInputStream();
				try {
					BufferedReader rd = new BufferedReader(new InputStreamReader(in,"UTF-8"));//这里可以用这个也可以直接用流,buffer的话就是多了个缓存功能
					int c = 0;
					StringBuffer temp = new StringBuffer();//请求接口获取json拼接字符串的话,比较长的返回结果String接收是不够的
					while((c = rd.read())!= -1){//这里可以用read也可用readLine,不清楚的话可以查一下两者区别
					       temp.append((char)c);
					}
					JSONObject jsonobject = JSONObject.fromObject(temp.toString());
					JSONArray dkobject = JSONArray.fromObject(jsonobject.get("recordresult"));//这里就是将json转成对象或者集合
					List<Object> list = (List<Object>) JSONArray.toCollection(dkobject,Object.class);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			} else {
				System.out.println(String.valueOf(conn.getResponseCode()));;
			}

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("发送POST请求出现异常!" + e);
		}
	}




文中用到的JSONObject和JSONArray的包是

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

可以自行下载导入,maven的话直接贴地址到pom.xml就好