我想将我的Flask应用程序转换为Google Cloud功能。函数是无状态的,我对应该如何做感到困惑。我已经简化了我的应用程序,因此如果有人可以帮助我将其简化为纯python,以便我可以将其部署为google cloud功能。
OAUTH = OAuth(APP)
MSGRAPH = OAUTH.remote_app(
'microsoft', consumer_key=config.CLIENT_ID, consumer_secret=config.CLIENT_SECRET,
request_token_params={'scope': config.SCOPES},
base_url=config.RESOURCE + config.API_VERSION + '/',
request_token_url=None, access_token_method='POST',
access_token_url=config.AUTHORITY_URL + config.TOKEN_ENDPOINT,
authorize_url=config.AUTHORITY_URL + config.AUTH_ENDPOINT)
@APP.route('/login')
def login():
"""Prompt user to authenticate."""
flask.session['state'] = str(uuid.uuid4())
return MSGRAPH.authorize(callback=config.REDIRECT_URI, state=flask.session['state'])
@APP.route('/login/authorized')
def authorized():
"""Handler for the application's Redirect Uri."""
if str(flask.session['state']) != str(flask.request.args['state']):
raise Exception('state returned to redirect URL does not match!')
response = MSGRAPH.authorized_response()
flask.session['access_token'] = response['access_token']
return return "200"
@MSGRAPH.tokengetter
def get_token():
"""Called by flask_oauthlib.client to retrieve current access token."""
return (flask.session.get('access_token'), '')
此应用程序无法很好地转换为Cloud Functions:您在每个路由之间共享会话状态,而Cloud Functions是无状态的。
根据您存储此状态的方式(即,如果它不仅在内存中),最好将应用程序部署到Cloud Run。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句