调用钉钉回调接口提示"errcode":71009,"errmsg":"返回文本非success, 本地可以解析出:"EventType" : "check_url"
代码是这么写的:
class DingTalkCrypto(): def init(self, encodingAesKey, key): self.encodingAesKey = encodingAesKey self.key = key self.aesKey = base64.b64decode(self.encodingAesKey + '=')
def encrypt(self, content):
"""加密"""
msg_len = self.length(content)
content = self.generateRandomKey(16) + msg_len.decode() + content + self.key
contentEncode = self.pks7encode(content)
iv = self.aesKey[:16]
aesEncode = AES.new(self.aesKey, AES.MODE_CBC, iv)
aesEncrypt = aesEncode.encrypt(contentEncode)
return base64.b64encode(aesEncrypt).decode().replace('\n', '')
def length(self, content):
"""将msg_len转为符合要求的四位字节长度"""
l = len(content)
return struct.pack('>l', l)
def pks7encode(self, content):
"""安装PKCS#7标准填充字符串"""
l = len(content)
output = io.StringIO()
val = 32 - (l % 32)
for _ in range(val):
output.write('%02x' % val)
return bytes(content, 'utf-8') + binascii.unhexlify(output.getvalue())
def pks7decode(self, content):
nl = len(content)
val = int(binascii.hexlify(content[-1].encode()), 16)
if val > 32:
raise ValueError('Input is not padded or padding is corrupt')
l = nl - val
return content[:l]
def decrypt(self, content):
"""解密数据"""
# 钉钉返回的消息体
content = base64.b64decode(content)
iv = self.aesKey[:16] # 初始向量
aesDecode = AES.new(self.aesKey, AES.MODE_CBC, iv)
decodeRes = aesDecode.decrypt(content)[20:].decode().replace(self.key, '')
return self.pks7decode(decodeRes)
def generateRandomKey(self, size,
chars=string.ascii_letters + string.ascii_lowercase + string.ascii_uppercase + string.digits):
"""生成加密所需要的随机字符串"""
return ''.join(choice(chars) for i in range(size))
def generateSignature(self, nonce, timestamp, token, msg_encrypt):
"""生成签名"""
signList = ''.join(sorted([nonce, timestamp, token, msg_encrypt])).encode()
return hashlib.sha1(signList).hexdigest()
def result_success(encode_aes_key, token, corp_id): dtc = DingTalkCrypto(encode_aes_key,corp_id) encrypt = dtc.encrypt('success') timestamp = str(int(round(time.time()))) nonce = dtc.generateRandomKey(8) # 生成签名 signature = dtc.generateSignature(nonce, timestamp, token, encrypt) # 构造返回数据 new_data = { 'data': { 'msg_signature': signature, 'timeStamp': timestamp, 'nonce': nonce, 'encrypt': encrypt } } return new_data
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。