首页 > 解决方案 > 如何将烧瓶应用程序转换为谷歌云功能

问题描述

我想将我的烧瓶应用程序转换为谷歌云功能。函数是无状态的,我不知道该怎么做。我已经简化了我的应用程序,因此如果有人可以帮助我将其简单地转换为纯 python,以便我可以将其部署为谷歌云功能。



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'), '')

标签: pythonflaskgoogle-cloud-platformgoogle-cloud-functions

解决方案


此应用程序无法很好地转换为 Cloud Functions:您在每个路由之间共享会话状态,而 Cloud Functions 是无状态的。

根据您存储此状态的方式(即,如果它不只是在内存中),您最好将应用程序部署到Cloud Run


推荐阅读