|
1 | 1 | # DiscordOAuth2.py
|
2 | 2 | Use Discord's OAuth2 effortlessly! Turns the auth code to a access token and the access token into scope infomation.
|
| 3 | + |
| 4 | +### Using DiscordOAut2.py with Flask |
| 5 | + |
| 6 | +```python |
| 7 | +from flask import Flask, redirect, request |
| 8 | +from threading import Thread |
| 9 | +import discordoauth2 as oauth2 |
| 10 | +import os |
| 11 | + |
| 12 | +app = Flask('') |
| 13 | +oauth2.client({ |
| 14 | + 'client_id': '849<example>993044', #the client id for your application |
| 15 | + 'client_secret': os.environ['oauth2_secret'], #the client secret for your application. Be super-extra-very-we-are-not-kidding-like-really-be-secure-make-sure-your-info-is-not-in-your-source-code careful with this. |
| 16 | + 'redirect_uri': 'https://example.com/oauth2', #the redirect for the uri below |
| 17 | + 'bot_token': os.environ['bot_token'] #the token for your app's bot, only required if you need to use guild.join |
| 18 | +}) |
| 19 | + |
| 20 | +oauth2_uri = "https://discord.com/api/oauth2/authorize?client_id=849<example>993044&redirect_uri=https%3A%2F%2Fexample.com%2Foauth2&response_type=code&scope=identify" |
| 21 | + |
| 22 | +@app.route('/oauth2') |
| 23 | +def oauth(): |
| 24 | + code = request.args.get('code') |
| 25 | + if not code: |
| 26 | + return redirect(oauth2_uri) |
| 27 | + else: |
| 28 | + token = oauth2.exchange_code(code) |
| 29 | + if str(token) == """{'error': 'invalid_request', 'error_description': 'Invalid "code" in request.'}""": |
| 30 | + return redirect(oauth2_uri) |
| 31 | + identify = oauth2.get_identify(token['access_token']) |
| 32 | + connections = oauth2.get_connections(token['access_token']) |
| 33 | + guilds = oauth2.get_guilds(token['access_token']) |
| 34 | + oauth2.join_guild(token['access_token'], 849912914450448395) |
| 35 | + return f'{identify}<br/><br/>{connections}<br/><br/>{guilds}' |
| 36 | + |
| 37 | +def run(): |
| 38 | + app.run(host="0.0.0.0", port=8080) |
| 39 | + |
| 40 | +server = Thread(target=run) |
| 41 | +server.start() |
| 42 | +``` |
0 commit comments