[Python] Trying to do discord auth without getting "CSRF Warning! State not equal in request...

Discussão em 'Python' iniciado por Stack, Abril 16, 2025.

  1. Stack

    Stack Membro Participativo

    Okay so I'm making my first ever desktop app using Electron and FastAPI but I've ran into a road block. I don't know if it's because I use Ubuntu as my main OS or I'm actually doing something wrong. I've tried asking ChatGPT to help but all it has gotten me to do is rewrite the auth/discord/callback && auth/discord like 10 times with the same error. I also tried state=None but that doesn't work either. If someone that knows more about Electron apps can explain what the issue could be that would be awesome.

    Please link some sources or if it can be found in the docs link me to where. (P.S. I've looked through the docs like 2 times so there is a chance I scrolled passed it)

    Here is what my backend code looks like for it right now

    oauth = OAuth()
    oauth.register(
    name='discord',
    client_id=os.getenv("DISCORD_CLIENT_ID"),
    client_secret=os.getenv("DISCORD_CLIENT_SECRET"),
    authorize_url='https://discord.com/api/oauth2/authorize',
    access_token_url='https://discord.com/api/oauth2/token',
    api_base_url='https://discord.com/api/',
    client_kwargs={'scope': 'identify email'},
    state=None
    )

    @app.get("/auth/discord")
    async def login_with_discord(request: Request):
    redirect_uri = os.getenv("DISCORD_REDIRECT_URI")
    return await oauth.discord.authorize_redirect(request, redirect_uri, state=None)


    @app.get("/auth/discord/callback")
    async def discord_callback(request: Request):
    try:
    token = await oauth.discord.authorize_access_token(request, state=None)
    except Exception as e:
    print(f"/auth/discord/callback Error: {e}")


    Here is my login.js:

    document.addEventListener("DOMContentLoaded", () => {
    const discordLoginButton = document.getElementById("discord-login");

    discordLoginButton.addEventListener("click", () => {
    // Redirect to the backend's Discord OAuth login endpoint
    const { shell } = require('electron');
    shell.openExternal("http://127.0.0.1:8000/auth/discord");

    });
    });

    Continue reading...

Compartilhe esta Página