├── README.md ├── main.py └── oauth.py /README.md: -------------------------------------------------------------------------------- 1 | # discord-oauth 2 | Example of Python code for Discord OAuth intergration for authentication/etc. 3 | 4 | All done via python, perhaps an unconventional language for Discord OAuth, but uses flask library primarily. 5 | Hopefully this will help with people wanting to implement Discord OAuth into their flask applications quickly! :) 6 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, redirect, session 2 | from oauth import oauth 3 | app = Flask(__name__) 4 | 5 | @app.route("/",methods = ['get']) 6 | def home(): 7 | return redirect(oauth.discord_login_url) 8 | 9 | @app.route("/login",methods = ['get']) 10 | def login(): 11 | code = request.args.get('code') 12 | access_token = oauth.get_accesstoken(code) 13 | user_json = oauth.get_userjson(access_token) 14 | print(user_json) 15 | username = user_json.get('username') 16 | discrim = user_json.get('discriminator') 17 | userid = user_json.get('id') 18 | return 'helllo world' 19 | 20 | if __name__ == "__main__": 21 | app.run(debug=True) 22 | -------------------------------------------------------------------------------- /oauth.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | class oauth(object): 4 | client_id = '' 5 | client_secret = '' 6 | scope = '' 7 | redirect_uri = 'http://127.0.0.1:5000/login' 8 | discord_login_url = f'https://discordapp.com/api/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope={scope}' 9 | discord_token_url = 'https://discordapp.com/api/oauth2/token' 10 | discord_api_url = 'https://discordapp.com/api' 11 | 12 | @staticmethod 13 | def get_accesstoken(code): 14 | payload = { 15 | 'client_id': oauth.client_id, 16 | 'client_secret': oauth.client_secret, 17 | 'grant_type': 'authorization_code', 18 | 'code': code, 19 | 'redirect_uri': oauth.redirect_uri, 20 | 'scope': oauth.scope 21 | } 22 | 23 | headers = { 24 | 'Content-Type': 'application/x-www-form-urlencoded' 25 | } 26 | 27 | access_token = requests.post(url = oauth.discord_token_url,data = payload,headers = headers) 28 | json = access_token.json() 29 | return json.get('access_token') 30 | 31 | @staticmethod 32 | def get_userjson(access_token): 33 | url = oauth.discord_api_url+'/users/@me' 34 | headers = { 35 | 'Authorisation': f'Bearer {access_token}' 36 | } 37 | 38 | user_obj = requests.get(url = url, headers = headers) 39 | user_json = user_obj.json() 40 | return user_json 41 | --------------------------------------------------------------------------------