123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import dingtalk.api
- class DingTalk():
- def __init__(self,appkey,appsecret,mobile):
- self.appkey = appkey
- self.appsecret = appsecret
- self.mobile = mobile
- def access_token(self):
- path = "https://oapi.dingtalk.com/gettoken"
- req= dingtalk.api.rest.OapiGettokenRequest(path)
- req.appkey=self.appkey
- req.appsecret=self.appsecret
- try:
- resp= req.getResponse()
- return resp["access_token"]
- except Exception as e:
- print(e)
- def getuserid_bymobile(self):
- path = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile"
- req = dingtalk.api.OapiV2UserGetbymobileRequest(path)
- req.mobile = self.mobile
- try:
- resp = req.getResponse(self.access_token())
- userid = resp["result"]["userid"]
- return userid
- except Exception as e:
- print(e)
- def username(self,userid):
- path = "https://oapi.dingtalk.com/topapi/v2/user/get"
- req = dingtalk.api.OapiV2UserGetRequest(path)
- req.userid = userid
- try:
- resp = req.getResponse(self.access_token())
- user_name = resp["result"]["name"]
- return user_name
- except Exception as e:
- print(e)
- if __name__ == '__main__':
- appkey = "dinglmkgry6mibab7tgs"
- appsecret = "DiJEEMHDt7Mk9X1eb_oFT4Vh-OIeRqN36HtIAbxD8M5Q6VGUvgsfzGRayRkpWO1t"
- mobile = "15727360513"
- Ding_Talk = DingTalk(appkey,appsecret,mobile)
- userid = Ding_Talk.getuserid_bymobile()
- username = Ding_Talk.username(userid)
- print(username,userid)
|