from flask import Flask import flask from alibabacloud_dingtalk.oauth2_1_0.client import Client as dingtalkoauth2_1_0Client from alibabacloud_tea_openapi import models as open_api_models from alibabacloud_dingtalk.oauth2_1_0 import models as dingtalkoauth_2__1__0_models from alibabacloud_tea_util.client import Client as UtilClient from alibabacloud_tea_util import models as util_models import dingtalk.api import json app = Flask(__name__) @app.route('/') def hello_world(): return 'flask_test is running!!!' @app.route('/getAccessToken') def login(): config = open_api_models.Config() config.protocol = 'https' config.region_id = 'central' client = dingtalkoauth2_1_0Client(config) # get_sso_access_token_request = dingtalkoauth_2__1__0_models.GetSsoAccessTokenRequest( # corpid='dingf113ce959247d417ee0f45d8e4f7c288', # sso_secret='09wTCSVdQMXDcqxGImreaAvvCfIF0BxfZsp393l_s9_XR5JZxcw3ha3zEm1LPq8e' # ) get_access_token_request = dingtalkoauth_2__1__0_models.GetAccessTokenRequest( app_key='dingsdbwmwnnnywdnk8e', app_secret='N8KZDtMQJWlTp-24s1TgN_jcHTf69SsyIaMyG0zwr7HlG7JY0cYIdVTbH4wwL8Rz' ) response = {} try: # access_token = client.get_sso_access_token(get_sso_access_token_request).body access_token = client.get_access_token(get_access_token_request).body print(access_token) response = {"accessToken": access_token.access_token, "expireIn": access_token.expire_in} return json.dumps(response) except Exception as err: if not UtilClient.empty(err.code) and not UtilClient.empty(err.message): # err 中含有 code 和 message 属性,可帮助开发定位问题 pass return json.dumps(response) @app.route('/getUserInfo', methods=['POST']) def get_user_info(): json_data = flask.request.json code = json_data["code"] accessToken = json_data["accessToken"] response = {} # config = open_api_models.Config() # config.protocol = 'https' # config.region_id = 'central' # client = dingtalkoauth2_1_0Client(config) # get_sso_user_info_headers = dingtalkoauth_2__1__0_models.GetSsoUserInfoHeaders() # get_sso_user_info_headers.x_acs_dingtalk_access_token = json_data["accessToken"] # get_sso_user_info_request = dingtalkoauth_2__1__0_models.GetSsoUserInfoRequest( # code=json_data["code"] # ) # response = {} # try: # user_info = client.get_sso_user_info_with_options(get_sso_user_info_request, get_sso_user_info_headers, # util_models.RuntimeOptions()).body # print(user_info) # return json.dumps(user_info) # except Exception as err: # if not UtilClient.empty(err.code) and not UtilClient.empty(err.message): # # err 中含有 code 和 message 属性,可帮助开发定位问题 # print(err.code) # print(err.message) # pass req = dingtalk.api.OapiV2UserGetuserinfoRequest("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo") req.code = code try: response = req.getResponse(accessToken) print(response) except Exception as e: print(e) return json.dumps(response) if __name__ == '__main__': app.run()