您的当前位置:首页正文

flask session密钥报错

2024-11-28 来源:个人技术集锦

1. 报错出现的代码:

self.authorize_ip = oauth.remote_app('oauth_ip', app_key='OAUTH_IP')
self.authorize_ip.authorized_response()

2. 报错信息如下:

E:\pythonPrj\CICD-TEST\venv\Scripts\python.exe E:/pythonPrj/CICD-TEST/flask_test.py
 * Serving Flask app 'app' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 338-955-284
 * Running on all addresses.
   WARNING: This is a development server. Do not use it in a production deployment.
 * Running on http://192.168.3.8:8084/ (Press CTRL+C to quit)
Access denied:error=The session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.

3. 报错根因:

  • session背景:flask中,用户登录后通过session的方式,存储登录的用户信息,便于网页中标识登录用户的信息,或携带用户的信息完成一些数据的持久化操作。
  • 错误来源:flask中使用session需要配置密钥,flask中则需要手动配置密钥。django会自动生成secret_key。

4. 解决措施:

  • 在配置文件中添加SECRET_KEY = ‘zidingyisecretkey’,直接复制我的添加到config文件里面就行。
import os
from celery.schedules import crontab


class Config(object):
    SECRET_KEY = 'zidingyisecretkey'
    APP_NAME = 'app'
    PROJECT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    SERVER_HOST = '192.168.3.8'
    SERVER_PORT = '8084'
    HOST_ADDRESS = 'http:{0}:{1}'.format(SERVER_HOST, SERVER_PORT)
    DEBUG = True
显示全文