Advertisement
NaroxEG

Discord OAuth2 Flask-Discord

Sep 27th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. from flask import Flask, redirect, url_for, render_template
  2. from flask_discord import DiscordOAuth2Session, requires_authorization, Unauthorized
  3. import os
  4.  
  5. os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
  6.  
  7. app = Flask(__name__)
  8. app.secret_key = 'YOUR-APP-SECRET'
  9.  
  10. app.config["DISCORD_CLIENT_ID"] = 0123456789
  11. app.config["DISCORD_CLIENT_SECRET"] = 'YOUR-CLIENT-SECRET'
  12. app.config["DISCORD_REDIRECT_URI"] = 'http://localhost:5000/callback'
  13.  
  14. discord = DiscordOAuth2Session(app)
  15.  
  16.  
  17. @app.route('/')
  18. def home():
  19.     return render_template('home.html')
  20.  
  21.  
  22. @app.route('/login')
  23. def login():
  24.     return discord.create_session(scope=["identify", "email"])
  25.  
  26.  
  27. @app.route('/callback')
  28. def callback():
  29.     discord.callback()
  30.     return redirect(url_for("dashboard"))
  31.  
  32.  
  33. @app.route('/dashboard')
  34. @requires_authorization
  35. def dashboard():
  36.     user = discord.fetch_user()
  37.     return render_template('dash.html', user=user)
  38.  
  39.  
  40. @app.errorhandler(Unauthorized)
  41. def redirect_unauthorized(e):
  42.     return redirect(url_for("login"))
  43.  
  44.  
  45. if __name__ == "__main__":
  46.     app.run(debug=True)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement