YzyApi.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import random
  4. import string
  5. import hashlib
  6. import time
  7. import base64
  8. import json
  9. import requests
  10. # YZY_HOST = "http://19.15.0.128:8080"
  11. YZY_HOST = "https://xtbg.digitalgd.com.cn"
  12. YZY_CORPID = "wld341060039"
  13. YZY_CORPSECRET = "5_8aOBBjioNbP7KDwjyBKmwnJ05-y1WbaJlt4irM1eA"
  14. YZH_PASSID = "yzy_demo"
  15. YZH_PASSTOKEN = "WjKat55cv6PrJtpCHld0trrHsv1mbCqL"
  16. dgd_pre_release = 1
  17. def get_access_token():
  18. url = "{}/ebus/yzyapi/cgi-bin/gettoken?corpid={}&corpsecret={}".format(YZY_HOST, YZY_CORPID, YZY_CORPSECRET)
  19. timestamp = str(int(time.time()))
  20. nonce = ranstr(20)
  21. signature = calcResponseSign(timestamp, YZH_PASSTOKEN, nonce)
  22. headers = {
  23. 'Content-Type': 'application/json;charset=UTF-8',
  24. "x-tif-signature": signature,
  25. "x-tif-timestamp": timestamp,
  26. "x-tif-nonce": nonce,
  27. "x-tif-paasid": YZH_PASSID,
  28. "dgd-pre-release": str(dgd_pre_release)
  29. }
  30. response = requests.get(url, headers=headers, timeout=15)
  31. print('yzy return:', response.text)
  32. def ranstr(num):
  33. salt = ''.join(random.sample(
  34. string.ascii_letters + string.digits, num))
  35. return salt
  36. #
  37. #
  38. # 生成校验码
  39. #
  40. #
  41. def authentication(timestamp, token, nonce, uid, uinfo, ext, signature):
  42. sign_data_sha256 = calcRequestSign(
  43. timestamp, token, nonce, uid, uinfo, ext)
  44. return sign_data_sha256 == signature.upper()
  45. #
  46. #
  47. # 计算校验码
  48. #
  49. #
  50. def calcResponseSign(timestamp, token, nonce):
  51. sign_data = "{}{}{}{}".format(timestamp, token, nonce, timestamp)
  52. return hashlib.sha256(
  53. sign_data.encode("utf8")
  54. ).hexdigest().upper()
  55. #
  56. #
  57. # 计算校验码
  58. #
  59. #
  60. def calcRequestSign(timestamp, token, nonce, uid, uinfo, ext):
  61. sign_data = "{}{}{},{},".format(
  62. timestamp, token, nonce, uid)
  63. if len(uinfo) == 0:
  64. sign_data = sign_data + ","
  65. else:
  66. sign_data = sign_data + uinfo + ","
  67. if len(ext) == 0:
  68. sign_data = sign_data
  69. else:
  70. sign_data = sign_data + ext
  71. sign_data = sign_data + timestamp
  72. return hashlib.sha256(
  73. sign_data.encode("utf8")
  74. ).hexdigest().upper()