钉钉api开发之读取审批流程表单数据
1、获取access_token
import requests
import dingtalk.api
import datetime
from datetime import datetime as dt
import dingtalk.api
import datetime
from datetime import datetime as dt
#先调用API获取到token
def getaccseetoken():
request = dingtalk.api.OapiGettokenRequest(
"https://oapi.dingtalk.com/gettoken")
request.corpid = "ding******"
request.corpsecret = "******"
f = request.getResponse()
access_token = f['access_token']
return access_token
print(getaccseetoken())
2、从审批流程页面获取procInstId
https://aflow.dingtalk.com/dingtalk/web/query/pchomepage.htm?from=oflow&op=true&corpid=ding********8#/plainapproval?procInstId=9*****f-af65-4828-b9e9-*******14
3、获取审批流程信息函数
def get_streaminfo(access_token, procinstid):
request = dingtalk.api.OapiProcessinstanceGetRequest(
"https://oapi.dingtalk.com/topapi/processinstance/get")
request.process_instance_id = procinstid
try:
resp = request.getResponse(access_token)
#print(resp)
return resp
except Exception as e:
print(e)
stream = get_streaminfo(getaccseetoken(),
'9******f-af65-4828-b9e9-7*********4')
print(stream)
print('审批编号',stream['process_instance']['business_id'])
print('申请日期',stream['process_instance']['form_component_values'][3]['value'])
print('申请部门',stream['process_instance']['form_component_values'][0]['value'])
#get_streaminfo(getaccseetoken(),'9******f-af65-4828-b***********4')
request = dingtalk.api.OapiProcessinstanceGetRequest(
"https://oapi.dingtalk.com/topapi/processinstance/get")
request.process_instance_id = procinstid
try:
resp = request.getResponse(access_token)
#print(resp)
return resp
except Exception as e:
print(e)
stream = get_streaminfo(getaccseetoken(),
'9******f-af65-4828-b9e9-7*********4')
print(stream)
print('审批编号',stream['process_instance']['business_id'])
print('申请日期',stream['process_instance']['form_component_values'][3]['value'])
print('申请部门',stream['process_instance']['form_component_values'][0]['value'])
#get_streaminfo(getaccseetoken(),'9******f-af65-4828-b***********4')
结果:
{'errcode': 0, 'errmsg': 'ok', 'process_instance': {'attached_process_instance_ids': [], 'biz_action': 'NONE', 'business_id': '202011091142000004747', 'create_time': '2020-11-09 11:42:01', 'finish_time': '2020-11-09 11:44:30', 'form_component_values': [{'component_type': 'DepartmentField', 'ext_value': '[{"number":20,"name":"景区管理部","id":403501094}]', 'id': 'DepartmentField_1QVZASNJL9340', 'name': '申请部门', 'value': '景区管理部'}, {'component_type': 'TextField', 'id': 'TextField_1TF1NPXZ30G00', 'name': '来访单位', 'value': '测试'}, {'component_type': 'TextField', 'id': 'TextField_1KQOH8KX6ECG0', 'name': '接待事由', 'value': '测试'}, {'component_type': 'DDDateField', 'id': 'DDDateField_1ZIJOV2BSGV40', 'name': '申请日期', 'value': '2020-11-09'}, {'component_type': 'DDDateField', 'id': 'DDDateField_1YXDZ14Y0HDS0', 'name': '接待日期', 'value': '2020-11-09'}, {'component_type': 'TableField', 'ext_value': '{"statValue":[{"id":"CalculateField_13ALAX6MRCG00","label":"总合计","num":"590","upper":""}],"componentName":"TableField"}', 'id': 'TableField_JBQSOQT4OI80', 'name': '用餐', 'value': '[{"rowValue":[{"componentType":"DDSelectField","label":"时间","extendValue":{"key":"option_0"},"value":"早上","key":"DDSelectField_8YW32MAFW440"},{"componentType":"TextField","label":"地点","value":"景区","key":"TextField_2R1LE8PM4TA0"},{"componentType":"NumberField","label":"接待人数","value":"9","key":"NumberField_11T9JTP4RO340"},{"componentType":"NumberField","label":"陪同人数","value":"0","key":"NumberField_11KQFW4XTM6O0"},{"componentType":"MoneyField","label":"接待标准 (元/人)","value":"50","key":"MoneyField_ZUCRTPCYDK00"},{"componentType":"CalculateField","label":"合计","value":"450","key":"CalculateField_13ALAX6MRCG00"}]},{"rowValue":[{"componentType":"DDSelectField","label":"时间","extendValue":{"key":"option_2"},"value":"晚上","key":"DDSelectField_8YW32MAFW440"},{"componentType":"TextField","label":"地点","value":"1","key":"TextField_2R1LE8PM4TA0"},{"componentType":"NumberField","label":"接待人数","value":"1","key":"NumberField_11T9JTP4RO340"},{"componentType":"NumberField","label":"陪同人数","value":"1","key":"NumberField_11KQFW4XTM6O0"},{"componentType":"MoneyField","label":"接待标准 (元/人)","value":"70","key":"MoneyField_ZUCRTPCYDK00"},{"componentType":"CalculateField","label":"合计","value":"140","key":"CalculateField_13ALAX6MRCG00"}]}]'}, {'component_type': 'TableField', 'ext_value': '{"statValue":[{"id":"CalculateField_3QA3THO6EB00","label":"总合计","num":"1200","upper":""}],"componentName":"TableField"}', 'id': 'TableField_Q0DNRB464ZK0', 'name': '住宿', 'value': '[{"rowValue":[{"componentType":"NumberField","label":"时间(天数)","value":"1","key":"NumberField_BZDX1FN12FC"},{"componentType":"TextField","label":"地点","value":"1","key":"TextField_22IJQLG27KF40"},{"componentType":"NumberField","label":"接待人数","value":"3","key":"NumberField_D9SZKK8RRM80"},{"componentType":"MoneyField","label":"金额(元)","value":"100","key":"MoneyField_1CUNACGWFMU80"},{"componentType":"CalculateField","label":"合计","value":"300","key":"CalculateField_3QA3THO6EB00"}]},{"rowValue":[{"componentType":"NumberField","label":"时间(天数)","value":"3","key":"NumberField_BZDX1FN12FC"},{"componentType":"TextField","label":"地点","value":"1","key":"TextField_22IJQLG27KF40"},{"componentType":"NumberField","label":"接待人数","value":"3","key":"NumberField_D9SZKK8RRM80"},{"componentType":"MoneyField","label":"金额(元)","value":"100","key":"MoneyField_1CUNACGWFMU80"},{"componentType":"CalculateField","label":"合计","value":"900","key":"CalculateField_3QA3THO6EB00"}]}]'}, {'component_type': 'CalculateField', 'ext_value': '{"upper":"壹仟柒佰玖拾元整","componentName":"MoneyField"}', 'id': 'CalculateField_18Z7VYNV31MO0', 'name': '申请总费用', 'value': '1790'}, {'component_type': 'DDPhotoField', 'id': 'DDPhotoField_3J9G3WENZTK0', 'name': '图片', 'value': 'null'}], 'operation_records': [{'date': '2020-11-09 11:42:00', 'operation_result': 'NONE', 'operation_type': 'START_PROCESS_INSTANCE', 'userid': 'manager5540'}, {'date': '2020-11-09 11:44:30', 'operation_result': 'AGREE', 'operation_type': 'EXECUTE_TASK_NORMAL', 'remark': '', 'userid': '081944650937626047'}], 'originator_dept_id': '403501094', 'originator_dept_name': '景区管理部', 'originator_userid': 'manager5540', 'result': 'agree', 'status': 'COMPLETED', 'tasks': [{'activity_id': '1918_5cd3', 'create_time': '2020-11-09 11:42:01', 'finish_time': '2020-11-09 11:44:30', 'task_result': 'AGREE', 'task_status': 'COMPLETED', 'taskid': '66175146413', 'url': 'aflow.dingtalk.com?procInsId=99a97f4f-af65-4828-b9e9-7bb044ea8f14&taskId=66175146413&businessId=202011091142000004747', 'userid': '081944650937626047'}], 'title': '***提交的***接待费申请审批表'}, 'request_id': '3iwlflbxnx5p'}
审批编号 202011091142000004747
申请日期 2020-11-09
申请部门 景区管理部
审批编号 202011091142000004747
申请日期 2020-11-09
申请部门 景区管理部
3、小结
(1)API获取钉钉信息不需要做内网穿透。
(2)corpid对应AppKey,corpsecret对应AppSecret,不要弄错,否则会出现“访问ip不在白名单之中”错误。
参看:https://www.dingtalk.com/qidian/help-detail-1000099475.html
访问ip不在白名单之中?(开放平台)
您好,当请求开放平台服务端接口遇到“访问ip不在白名单”问题时,表示您的请求ip不在该应用出口ip白名单内。可以按照以下方式排查并修改:
(1)对比调用接口的应用中的appkey值,与报错信息中返回的appkey值是否一致。如果不一致,可能是由于apkey或者appsecret使用的是其他的,导致在获取accessToken值时会提示错误。注意:如果错误信息中返回的是corpid值,说明创建应用时间较早,添加ip的路径为:登录开发者后台-基本信息-开发信息旧版-找到该应用使用的Corpsecret值,点击设置修改ip;如果当前账号按照上述路径查看不到Corpsecret,需要改企业主管理员账号查看并修改ip;
(2)如果应用中的appkey值跟报错信息中返回的appkey值是一致的,解决办法是将报错信息中的request ip添加到该应用的出口ip里面。您可以点击此处查看相关文档。