Python钉钉企业内部接口开发,使用Post请求数据报错500如何解决
我用 Python 来处理钉钉的服务器端接口
现在遇到一个问题,申请 token 后,get 请求都是能用的,post 数据就会报 500 错误
之前发的接口数据字段多,怕参数哪里写的不对,于是使用了(获取用户待审批数量)接口测试找原因,这个接口只需要传递一个 userid
https://open-doc.dingtalk.com/microapp/serverapi2/ui5305
access_token = "xxxxxxxxxx"
url = 'https://oapi.dingtalk.com/topapi/process/gettodonum?access_token=%s' % access_token
data = {
“userid”: “manager4012”
}
headers = {
‘Content-Type’: ‘application/json’
}
r = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)
报错:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white"><script>
with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","category=&userid=&aplus&yunid=&asid=AQAACADXmhdb3r3FKQAACACKRiLMGAxPpg==",id="tb-beacon-aplus",src="//g.alicdn.com/alilog/mlog/aplus_v2.js")
</script>
<h1>500 Internal Server Error</h1>
<p>The server encountered an internal error or misconfiguration and was unable to complete your request.</body>
headers utf-8 也指定了,json 字符串也转换了
纠结一两天,文档,demo 找了不少,很是不能理解一个 post 过去直接 500 错误,没思路了,特来求助
Python钉钉企业内部接口开发,使用Post请求数据报错500如何解决
500 是他们服务端问题
500错误是服务器内部错误,需要先定位问题出在服务端还是客户端。我处理钉钉接口时也常遇到,通常按这个顺序排查:
- 检查请求参数:钉钉接口对参数格式要求严格,特别是时间戳和签名。
import time
import hmac
import hashlib
import urllib.parse
import requests
def get_dingtalk_signature(secret):
"""生成钉钉签名"""
timestamp = str(round(time.time() * 1000))
string_to_sign = f"{timestamp}\n{secret}"
hmac_code = hmac.new(
secret.encode('utf-8'),
string_to_sign.encode('utf-8'),
digestmod=hashlib.sha256
).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return timestamp, sign
# 示例请求
access_token = "你的access_token"
secret = "你的应用secret"
timestamp, sign = get_dingtalk_signature(secret)
url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"
params = {
"access_token": access_token,
"timestamp": timestamp,
"sign": sign
}
headers = {
"Content-Type": "application/json"
}
data = {
"agent_id": 123456,
"userid_list": "user1,user2",
"msg": {
"msgtype": "text",
"text": {"content": "测试消息"}
}
}
response = requests.post(url, params=params, json=data, headers=headers)
print(f"状态码: {response.status_code}")
print(f"响应内容: {response.text}")
-
验证access_token:确保token未过期且权限正确。
-
检查请求体格式:钉钉要求JSON格式,注意嵌套结构。
-
查看钉钉返回的具体错误信息:500错误时响应体通常会有详细错误码和消息。
-
尝试简化请求:先用最小参数集测试,逐步添加参数定位问题参数。
建议先用最简单的请求测试基础连通性。
是这么个理儿,但是… 他们不检测自己 API 可用性的吗…
不确定钉钉的 access_token 什么格式,但是这段代码唯一有问题的可能就是 access_token 没进行 URLEncode
国内的开放 API 都这尿性
喂屎💩给别人吃,以后你会遇到更多
不是 KPI 的事情,完全没动力做好

