Discord's OAuth2 integration allows developers to authenticate users through their Discord accounts, providing a seamless login experience while gaining access to user profile data. In this guide, we'll walk through implementing Discord social login in a Python web application using Flask.
Setting Up Your Discord Application
- Go to the Discord Developer Portal.
- Click "New Application" and give it a name.
- Navigate to the "OAuth2" section.
- Note your Client ID and generate a Client Secret.
- Add a redirect URI (e.g., http://127.0.0.1:5000/callback).
Install the required dependencies by creating a requirements.txt file.
discord.py==2.5.2
Flask==3.1.0
python-dotenv==1.0.1
requests==2.32.3
requests-oauthlib==2.0.0
flasgger==0.9.7.1
Configuration
Create a config.py file to manage your configuration.
import os
from dotenv import load_dotenv
load_dotenv()
class Config:
CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
REDIRECT_URI = os.getenv('DISCORD_REDIRECT_URI')
AUTHORIZATION_BASE_URL = os.getenv('AUTHORIZATION_BASE_URL')
TOKEN_URL = os.getenv('TOKEN_URL')
SCOPE = os.getenv('SCOPE').split(',')
Create a .env file with your Discord credentials.
CLIENT_ID=your_client_id_here
CLIENT_SECRET=your_client_secret_here
DISCORD_REDIRECT_URI=http://127.0.0.1:5000/callback
AUTHORIZATION_BASE_URL=https://discord.com/api/oauth2/authorize
TOKEN_URL=https://discord.com/api/v10/oauth2/token
SCOPE=identify,email
Implementing the Flask Application
Here's the complete implementation (app.py) with explanations for each part.
from flask import Flask, redirect, request, session
from requests_oauthlib import OAuth2Session
import os
from dotenv import load_dotenv
import requests
from flasgger import Swagger
from config import Config
load_dotenv()
app = Flask(__name__)
app.secret_key = os.urandom(24)
# Swagger configuration for API documentation
app.config['SWAGGER'] = {
'title': 'Discord Login API',
'uiversion': 3
}
swagger = Swagger(app)
1. Login Route
This route initiates the OAuth2 flow by redirecting users to Discord's authorization page.
@app.get('/login')
def login():
"""
Redirects user to Discord for authorization.
---
responses:
302:
description: Redirect to Discord's authorization page.
"""
discord = OAuth2Session(
Config.CLIENT_ID,
redirect_uri=Config.REDIRECT_URI,
scope=Config.SCOPE
)
authorization_url, state = discord.authorization_url(Config.AUTHORIZATION_BASE_URL)
session['oauth_state'] = state
return redirect(authorization_url)

Join the conversation! Your thoughts help the community grow.