DingTalk.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import dingtalk.api
  2. class DingTalk():
  3. def __init__(self,appkey,appsecret,mobile):
  4. self.appkey = appkey
  5. self.appsecret = appsecret
  6. self.mobile = mobile
  7. def access_token(self):
  8. path = "https://oapi.dingtalk.com/gettoken"
  9. req= dingtalk.api.rest.OapiGettokenRequest(path)
  10. req.appkey=self.appkey
  11. req.appsecret=self.appsecret
  12. try:
  13. resp= req.getResponse()
  14. return resp["access_token"]
  15. except Exception as e:
  16. print(e)
  17. def getuserid_bymobile(self):
  18. path = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile"
  19. req = dingtalk.api.OapiV2UserGetbymobileRequest(path)
  20. req.mobile = self.mobile
  21. try:
  22. resp = req.getResponse(self.access_token())
  23. userid = resp["result"]["userid"]
  24. return userid
  25. except Exception as e:
  26. print(e)
  27. def username(self,userid):
  28. path = "https://oapi.dingtalk.com/topapi/v2/user/get"
  29. req = dingtalk.api.OapiV2UserGetRequest(path)
  30. req.userid = userid
  31. try:
  32. resp = req.getResponse(self.access_token())
  33. user_name = resp["result"]["name"]
  34. return user_name
  35. except Exception as e:
  36. print(e)
  37. if __name__ == '__main__':
  38. appkey = "dinglmkgry6mibab7tgs"
  39. appsecret = "DiJEEMHDt7Mk9X1eb_oFT4Vh-OIeRqN36HtIAbxD8M5Q6VGUvgsfzGRayRkpWO1t"
  40. mobile = "15727360513"
  41. Ding_Talk = DingTalk(appkey,appsecret,mobile)
  42. userid = Ding_Talk.getuserid_bymobile()
  43. username = Ding_Talk.username(userid)
  44. print(username,userid)