Skip to content

Fix flask initdb error: migrate to Authlib 1.x#26

Open
wadelhw wants to merge 2 commits intoauthlib:masterfrom
wadelhw:fix/19-flask-initdb-error
Open

Fix flask initdb error: migrate to Authlib 1.x#26
wadelhw wants to merge 2 commits intoauthlib:masterfrom
wadelhw:fix/19-flask-initdb-error

Conversation

@wadelhw
Copy link
Copy Markdown

@wadelhw wadelhw commented Apr 1, 2026

Summary

Fixes #19flask initdb fails with ImportError: cannot import name '_app_ctx_stack' from 'flask' when using modern Flask (3.x) with Authlib 0.13.

Root Cause

Authlib 0.13 imports flask._app_ctx_stack, which was removed in Flask 2.3+. The unpinned Flask and Flask-SQLAlchemy dependencies resolve to Flask 3.1.x and Flask-SQLAlchemy 3.1.x, causing the import failure at app startup.

Changes

requirements.txt

  • Updated Authlib==0.13Authlib>=1.0

website/oauth2.py — Migrated to Authlib 1.x API:

  • create_authorization_codesave_authorization_code (new signature: code + request instead of client + grant_user + request)
  • parse_authorization_codequery_authorization_code
  • exists_nonce now uses request.payload.client_id (Authlib 1.x request structure)
  • get_jwt_config now receives client parameter for OpenIDCode, ImplicitGrant, HybridGrant
  • Authorization code helper updated for new request object attributes (request.client, request.payload, request.user)

website/routes.py

  • Fixed Blueprint(__name__, 'home')Blueprint('home', __name__) (Flask 3.x rejects dots in Blueprint names)
  • validate_consent_requestget_consent_grant (Authlib 1.x rename)

Testing

  • pip install -r requirements.txt installs successfully (Authlib 1.6.9, Flask 3.1.3, Flask-SQLAlchemy 3.1.1)
  • flask initdb runs without errors and creates the SQLite database
  • App imports and all OAuth2 routes register correctly

wadelhw and others added 2 commits April 2, 2026 03:06
Authlib 0.13 imports flask._app_ctx_stack which was removed in Flask 2.3+.
Bumping to Authlib>=1.0 resolves the ImportError on modern Flask versions.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Adapts oauth2.py and routes.py to Authlib 1.x breaking changes:
- create_authorization_code -> save_authorization_code (new signature)
- parse_authorization_code -> query_authorization_code
- validate_consent_request -> get_consent_grant
- get_jwt_config now receives client parameter
- request object uses .payload and .client attributes
- Blueprint name fixed for Flask 3.x (no dots allowed)

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error while running flask initdb

1 participant