auth_launch.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: UTF-8 -*-
  2. import esigntool
  3. import requests
  4. from esigntool import esign_run_print_outer
  5. # 认证和授权服务API - 实名认证 - 发起类
  6. config = esigntool.Config() # 初始化配置类
  7. @esign_run_print_outer
  8. def getPsnAuthUrl(mobile):
  9. """
  10. 获取个人认证&授权页面链接
  11. :param mobile:实名用户手机号
  12. :return:请求响应
  13. """
  14. api_path = "/v3/psn-auth-url"
  15. method = esigntool.httpMethodEnum.POST
  16. body = {
  17. "clientType": "ALL", # 自动适配移动端或PC端
  18. "authorizeConfig": { # 实名认证可以不设置此参数,授权可以默认所有权限都设置
  19. "authorizedScopes": ["get_psn_identity_info", "psn_initiate_sign", "manage_psn_resource"]
  20. },
  21. "redirectConfig": {
  22. "redirectUrl": "https://www.baidu.com"
  23. },
  24. "psnAuthConfig": {
  25. "psnAccount": mobile,
  26. "psnInfo": {
  27. "psnName": ""
  28. }
  29. }
  30. }
  31. if mobile == "":
  32. print("请设置实名用户个人手机号")
  33. exit()
  34. # 签名并构造签名鉴权json请求头
  35. json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  36. # 发起请求
  37. resp = requests.request(method, config.host + api_path, json=body, headers=json_headers)
  38. print(resp.text)
  39. return resp.text
  40. # 获取机构认证 & 授权页面链接
  41. @esign_run_print_outer
  42. def getOrgAuthUrl(mobile, orgName):
  43. """
  44. :param mobile:实名用户手机号
  45. :param orgName:实名企业名称
  46. :return:请求响应
  47. """
  48. api_path = "/v3/org-auth-url"
  49. method = esigntool.httpMethodEnum.POST
  50. body = {
  51. "clientType": "ALL", # 自动适配移动端或PC端
  52. "authorizeConfig": { # 实名认证可以不设置此参数,授权可以默认所有权限都设置
  53. "authorizedScopes": ["get_org_identity_info", "get_psn_identity_info", "org_initiate_sign",
  54. "psn_initiate_sign", "manage_org_resource", "manage_psn_resource", "use_org_order"]
  55. },
  56. "redirectConfig": {
  57. "redirectUrl": "https://www.baidu.com"
  58. },
  59. "orgAuthConfig": {
  60. "orgName": orgName,
  61. "transactorInfo": {
  62. "psnAccount": mobile
  63. }
  64. }
  65. }
  66. if mobile == "" or orgName == "":
  67. print("请设置实名用户个人手机号和实名企业的名称")
  68. exit()
  69. # 签名并构造签名鉴权json请求头
  70. json_headers = esigntool.buildSignJsonHeader(config.appId, config.scert, method, api_path, body=body)
  71. # 发起请求
  72. resp = requests.request(method, config.host + api_path, json=body, headers=json_headers)
  73. print(resp.text)
  74. return resp.text
  75. if __name__ == '__main__':
  76. if config.appId == "" or config.scert == "":
  77. print("请设置应用appId和应用Secret")
  78. exit()
  79. mobile = "" # 请输入个人联系手机号
  80. orgName = "" # 请输入企业名称
  81. getPsnAuthUrl(mobile) # 获取个人认证&授权页面链接
  82. getOrgAuthUrl(mobile, orgName) # 获取机构认证 & 授权页面链接