app.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from flask import Flask
  2. import flask
  3. from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client
  4. from alibabacloud_tea_openapi import models as open_api_models
  5. from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models
  6. from alibabacloud_tea_util.client import Client as UtilClient
  7. from alibabacloud_tea_util import models as util_models
  8. import dingtalk.api
  9. import json
  10. app = Flask(__name__)
  11. @app.route('/')
  12. def hello_world():
  13. return 'flask_test is running!!!'
  14. @app.route('/getAccessToken')
  15. def login():
  16. config = open_api_models.Config()
  17. config.protocol = 'https'
  18. config.region_id = 'central'
  19. client = dingtalkoauth2_1_0Client(config)
  20. # get_sso_access_token_request = dingtalkoauth_2__1__0_models.GetSsoAccessTokenRequest(
  21. # corpid='dingf113ce959247d417ee0f45d8e4f7c288',
  22. # sso_secret='09wTCSVdQMXDcqxGImreaAvvCfIF0BxfZsp393l_s9_XR5JZxcw3ha3zEm1LPq8e'
  23. # )
  24. get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest(
  25. app_key='dingsdbwmwnnnywdnk8e',
  26. app_secret='N8KZDtMQJWlTp-24s1TgN_jcHTf69SsyIaMyG0zwr7HlG7JY0cYIdVTbH4wwL8Rz'
  27. )
  28. response = {}
  29. try:
  30. # access_token = client.get_sso_access_token(get_sso_access_token_request).body
  31. access_token = client.get_access_token(get_access_token_request).body
  32. print(access_token)
  33. response = {"accessToken": access_token.access_token, "expireIn": access_token.expire_in}
  34. return json.dumps(response)
  35. except Exception as err:
  36. if not UtilClient.empty(err.code) and not UtilClient.empty(err.message):
  37. # err 中含有 code 和 message 属性,可帮助开发定位问题
  38. pass
  39. return json.dumps(response)
  40. @app.route('/getUserInfo', methods=['POST'])
  41. def get_user_info():
  42. json_data = flask.request.json
  43. code = json_data["code"]
  44. accessToken = json_data["accessToken"]
  45. response = {}
  46. # config = open_api_models.Config()
  47. # config.protocol = 'https'
  48. # config.region_id = 'central'
  49. # client = dingtalkoauth2_1_0Client(config)
  50. # get_sso_user_info_headers = dingtalkoauth_2__1__0_models.GetSsoUserInfoHeaders()
  51. # get_sso_user_info_headers.x_acs_dingtalk_access_token = json_data["accessToken"]
  52. # get_sso_user_info_request = dingtalkoauth_2__1__0_models.GetSsoUserInfoRequest(
  53. # code=json_data["code"]
  54. # )
  55. # response = {}
  56. # try:
  57. # user_info = client.get_sso_user_info_with_options(get_sso_user_info_request, get_sso_user_info_headers,
  58. # util_models.RuntimeOptions()).body
  59. # print(user_info)
  60. # return json.dumps(user_info)
  61. # except Exception as err:
  62. # if not UtilClient.empty(err.code) and not UtilClient.empty(err.message):
  63. # # err 中含有 code 和 message 属性,可帮助开发定位问题
  64. # print(err.code)
  65. # print(err.message)
  66. # pass
  67. req = dingtalk.api.OapiV2UserGetuserinfoRequest("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo")
  68. req.code = code
  69. try:
  70. response = req.getResponse(accessToken)
  71. print(response)
  72. except Exception as e:
  73. print(e)
  74. return json.dumps(response)
  75. if __name__ == '__main__':
  76. app.run()