OAuth2 code_verifier missing

I wanted to create my Oauth2 provider for authentication in my django applications. For the test I used Django OAuth Toolkit and created 2 separate projects on different ports:
http://127.0.0.1:8001 – client
http://127.0.0.1:8000 – provider

Here are all the endpoints for the provider that are redirected to for login and access confirmation:

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
    path('admin/', admin.site.urls),
    path('o/', include('oauth2_provider.urls', namespace='oauth2_provider')),
    path('accounts/login/', auth_views.LoginView.as_view(template_name='login.html'), name='login'),
    path('accounts/logout/', auth_views.LogoutView.as_view(), name='logout'),

The code for the login page:

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Login</button>
    </form>
</body>
</html>

On the provider’s side everything goes normally and I get to the page where I confirm the user and I am redirected to a special page of the callback client.

Client endpoints:

from django.contrib import admin
from django.urls import path
from client import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('client/login/', views.login, name='login'),
    path('client/callback/', views.callback, name='callback'),
]

Client Views:

import os
import string
import random
import base64
import hashlib

from django.http import HttpResponse
from django.shortcuts import redirect, render
from requests_oauthlib import OAuth2Session

os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

CLIENT_ID = 'hR0E2tAkf8mLqOwNpPPun0dTBDU44JF8z8SgJfnJ'
CLIENT_SECRET = 'hNi3IugtuqtEFa5GonCW4ilQTnsjz2nUE1jf2WeoxaMQNZHt1pzszLGeGtpYsNXV7VHouFF79C7XMOmSJRcXrY4OEPoJuTQyIng6ONjjt4tPvJfpPJpKMFPX9MPgUpps'
REDIRECT_URI = 'http://127.0.0.1:8001/client/callback/'
AUTHORIZATION_URL = 'http://127.0.0.1:8000/o/authorize/'
TOKEN_URL = 'http://127.0.0.1:8000/o/token/'


def generate_code_verifier():
    verifier = ''.join(random.choices(string.ascii_letters + string.digits, k=128))
    return base64.urlsafe_b64encode(verifier.encode('utf-8')).rstrip(b'=').decode('utf-8')


def generate_code_challenge(verifier):
    code_challenge = hashlib.sha256(verifier.encode('utf-8')).digest()
    return base64.urlsafe_b64encode(code_challenge).rstrip(b'=').decode('utf-8')


def login(request):
    oauth = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI)
    code_verifier = generate_code_verifier()
    code_challenge = generate_code_challenge(code_verifier)
    authorization_url, state = oauth.authorization_url(
        AUTHORIZATION_URL,
        code_challenge=code_challenge,
        code_challenge_method='S256'
    )

    request.session['code_verifier'] = code_verifier
    print(f"Saved code_verifier in session: {request.session['code_verifier']}")
    return redirect(authorization_url)


def callback(request):
    code = request.GET.get('code')

    code_verifier = request.session.get('code_verifier')
    if code_verifier is None:
        return HttpResponse("Code verifier is missing in the session.")

    print(f"Retrieved code_verifier from session: {code_verifier}")
    oauth = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI)
    try:
        token = oauth.fetch_token(
            TOKEN_URL,
            code=code,
            client_secret=CLIENT_SECRET,
            code_verifier=code_verifier
        )
        access_token = token.get('access_token')
        return render(request, 'callback.html', {'access_token': access_token})
    except Exception as e:
        print(f"Error fetching token: {e}")
        return HttpResponse(f"Error fetching token: {e}")

In the method that is responsible for login on the client side I get code_verifier and it is even displayed in the console, but when I go to callback.html I cannot get it and accordingly I get: “Code verifier is missing in the session”.

I don’t know what I did wrong and why it doesn’t save and I can’t retrieve it? If you need more information I will give it to you.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật