Python3.7使用钉钉2.x版本接口(取token发钉钉消息)

钉钉开放平台服务端API下载地址 https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02

钉钉的python包是适配python2版本的,如果在python3下使用会报错。经过修改后测试获取钉钉token及发送应用消息成功!

Python SDK需要依赖python2

一、下载钉钉python sdk后使用python3自带的2to3功能进行转换

参考地址: https://zhidao.baidu.com/question/1112843826603079459.html

找到python3的安装目录 如 C:\Python27\Tools\Scripts\2to3.py

打开cmd切换到2to3.py所在目录: 输入“python 2to3.py -w ”命令后不要急着回车,先把你要转换的python文件或目录文件夹直接拖到cmd窗口中,他会自动填充成“ python 2to3.py -w C:\Users\Administrator\Desktop\python\dingding3”然后回车即可。我的sdk是解压到桌面的“ C:\Users\Administrator\Desktop\python\dingding3 ”目录的,转换的同时会生成.bak备份文件

二、修改钉钉python2版本sdk的代码

报错:TypeError: getsockaddrarg: AF_INET address must be tuple, not int ,其实是http.client.HTTPSConnection与http.client.HTTPConnection多了参数

修改base.py文件 dingding3\dingtalk\api\base.py

找到如下代码
connection = http.client.HTTPSConnection(self.__domain, self.__port, None, None, False, timeout)
改为
connection = http.client.HTTPSConnection(self.__domain, self.__port,None,None,timeout)

找到如下代码
connection = http.client.HTTPConnection(self.__domain, self.__port, False, timeout)
改为
connection = http.client.HTTPConnection(self.__domain, self.__port,timeout)

另外注意tab与空格对齐的问题!部分代码行会报错“IndentationError: unindent does not match any outer indentation level”,如果你用的是vscode可以安装“Trailing Space”插件,然后把vscode的配置修改下打开setting,搜索renderControlCharacters 勾选,renderWhitespace设置为all(参考: https://blog.csdn.net/u014001964/article/details/93993461 )可以明显看到tab与空格的不同。

三、开始测试,在dingding3目录下新建一个testdingding.py

import dingtalk.api
import requests
#import ssl
import json
import datetime


request = dingtalk.api.OapiGettokenRequest("https://oapi.dingtalk.com/gettoken")
#request.corpid="dingoahrq2nlfy7ka6"
#request.corpsecret="TGwtkyRvtoqAm1ttQ2WtrkPkn9wpfFaQIfqY9960bxli3nvq2kHAtXqQOuuM"
request.appkey="dingql1xm9van2xd"
request.appsecret="1bAytrjRNeRrIx1UfnIFppsrdySkduVEk1PmwU890D69GAO46IOBJtKnIgh"

result = request.getResponse()
token_get = result['access_token']
print('获取到的token:',token_get)

#消息接口
request = dingtalk.api.OapiMessageCorpconversationAsyncsendV2Request("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+token_get)
#h5 微应用的agent_id
request.agent_id = 472999813

#需要发送的用户userid
request.userid_list = 'manager244'

#消息内容
request.msg = {"msgtype":"text","text":{"content":"消息内容: 系统登录时间 "+ str(datetime.datetime.now())}}
result = request.getResponse()
print('消息发送流水号:'+str(result["task_id"]))


"""
#直接调用接口(非SDK形式)

token_get = '206c33ca2d523a2e9362a7944fa55d20'

if (token_get == ''):

    url = "https://oapi.dingtalk.com/gettoken"
    params = {"appkey":"dingql1xm9van2xd","appsecret":"1bAytrjRNeRrIx1UfnIFppsrdySkduVEk1PmwU890D69GAO46IOBJtKnIghi"}
    res = requests.get(url=url,params=params)
    #返回str
    result = json.loads(res.text)
    token_get = result["access_token"]


print(token_get)

#开始发送钉钉消息
url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token="+token_get
params = {"agent_id":"472999813","userid_list":"manager244","msg":'{"msgtype":"text","text":{"content":"消息内容s"}}'}
res = requests.post(url=url,params=params)
#返回str
result = json.loads(res.text)
task_id = result["task_id"]

print(task_id)

"""

如果上面不行可以尝试升级pip本身,在dos命令行下运行: python -m pip install –upgrade pip

安装request包:pip install request

至此,可以灵活运用钉钉的python sdk来实现相关功能了。

基于互联网精神,在注明出处的前提下本站文章可自由转载!

本文链接:https://ranjuan.cn/python3-7使用钉钉2-x版本接口/

赞赏

微信赞赏支付宝赞赏

solve-network-error691
alioss-upload-policy
专业版windows系统bitlocker突然无法打开

目前为止有一条评论

KAIRT 发布于下午11:18 - 2020年3月15日

博主还在吗?