钉钉审批接口调用

最近走访了一些客户,发现移动办公使用 企业微信和钉钉的越来越多。所以获取审批数据、打卡信息等就不用导出了,我们可以通过官方接口直接获取。比如应用到人力部门的考勤核算中。

这里就以获取【审批】流程里的【请假单】为例:

1. 获取 Access_Token,

1.1 注册企业应用 ,获取 AppKey、AppSecret

参考链接: https://open-doc.dingtalk.com/microapp/serverapi2/eev437

1.2 添加获取 getAccessToken 方法,获取 Access_Token

添加引用

import urllib.request
import time
import json
import os

def getAccessToken(_appkey,_appsecret):
    
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }
    url = 'https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s' % (_appkey, _appsecret)
    req = urllib.request.Request(url, headers=headers)
    result = urllib.request.urlopen(req)
    access_token = json.loads(result.read())['access_token']
    timeStamp = str(time.time()).split('.')[0]
    return access_token,timeStamp

注意时间戳的格式:str(time.time()).split(‘.’)[0],直接用 time.time 会报错

2 获取审批列表信息

2.1 审批列表请求地址

请求地址: https://oapi.dingtalk.com/topapi/processinstance/listids?access_token=ACCESS_TOKEN

2.2 进程号

打开【审批】新增一张【请假单】,在地址栏里能找到 process_code

例如:钉钉审批接口调用

process_code = PROC-CA7AA2BB-9241-45CC-9813-01A79DD15CBF

2.3 添加获取列表的方法

def GetListIds(access_token,timeStamp):
    accesstoken = access_token
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }

    process_code = PROC-CA7AA2BB-9241-45CC-9813-01A79DD15CBF'
    data = {'process_code': process_code,
            'start_time': timeStamp}  # 时间戳

    data1 = json.dumps(data).encode(encoding='UTF8')
    url = 'https://oapi.dingtalk.com/topapi/processinstance/listids?access_token=%s' % (accesstoken)
    req = urllib.request.Request(url, headers=headers, data=data1)
    result = urllib.request.urlopen(req)
    list = json.loads(result.read())
    return list

3 获取单个审批实例

审批实例 id:process_instance_id,可以从上面的列表里获取

def GetUserInfo(_processID,_accessToken):
    
    headers = {
        'Content-Type': 'application/json',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36'
    }

    
    data = {'process_instance_id': _processID}

    data1 = json.dumps(data).encode(encoding='UTF8')
    url = 'https://oapi.dingtalk.com/topapi/processinstance/get?access_token=%s' % (_accessToken)
    req = urllib.request.Request(url, headers=headers, data=data1)
    result = urllib.request.urlopen(req)
    list = json.loads(result.read())
    return list

4 调用

if __name__ == '__main__':
  appkey = '*'
  appsecret = '*'

  # 获取token
  token_timeStamp = getAccessToken(appkey,appsecret)

  #获取列表
  list_id = GetListIds(oken_timeStamp[0],token_timeStamp[1])[''result'']["list"]
	  # 遍历list_id,逐条获取单个审批实例
	  for i in range(len(list_id)):
		  user_info = GetUserInfo(list_id[i],token_timeStamp[0])

最后

我这该死的无处安放的魅力 !! ———— 图腾