Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, redirect, url_for, render_template, request, session
- import requests
- import os
- from urllib.parse import urlencode
- app = Flask(__name__)
- app.secret_key = os.urandom(24)
- CLIENT_ID = "CLIENT_ID"
- CLIENT_SECRET = "CLIENT_SECRET"
- REDIRECT_URI = "http://localhost:5000/callback"
- DISCORD_API_ENDPOINT = "https://discord.com/api/v10/"
- @app.route('/')
- def home():
- return render_template('home.html')
- @app.route('/login')
- def login():
- params = {
- "client_id": CLIENT_ID,
- "redirect_uri": REDIRECT_URI,
- "response_type": "code",
- "scope": "identify email"
- }
- return redirect(f"https://discord.com/api/oauth2/authorize?{urlencode(params)}")
- @app.route('/callback')
- def callback():
- code = request.args.get("code")
- if not code:
- return "No code provided", 400
- data = {
- "client_id": CLIENT_ID,
- "client_secret": CLIENT_SECRET,
- "grant_type": "authorization_code",
- "code": code,
- "redirect_uri": REDIRECT_URI
- }
- response = requests.post(f"{DISCORD_API_ENDPOINT}/oauth2/token", data=data)
- if response.status_code != 200:
- return "Failed to get access token", 400
- token_data = response.json()
- session["access_token"] = token_data["access_token"]
- return redirect(url_for('dashboard'))
- @app.route('/dashboard')
- def dashboard():
- if "access_token" not in session:
- return redirect(url_for('login'))
- session_token = session["access_token"]
- headers = {
- "Authorization": f"Bearer {session_token}"
- }
- response = requests.get(f"{DISCORD_API_ENDPOINT}/users/@me", headers=headers)
- if response.status_code != 200:
- return "Failed to get user information", 400
- user_data = response.json()
- return render_template("dash.html", user=user_data)
- if __name__ == "__main__":
- app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement