|
2 | 2 | Use Discord's OAuth2 effortlessly! Turns the auth code to a access token and the access token into scope infomation. |
3 | 3 |
|
4 | 4 | ### Using DiscordOAut2.py with Flask |
5 | | - |
6 | 5 | ```python |
7 | 6 | from flask import Flask, redirect, request |
8 | 7 | from threading import Thread |
9 | | -import discordoauth2 as oauth2 |
| 8 | +from discordoauth2 import discordOauth2 |
10 | 9 | import os |
11 | 10 |
|
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 | | -}) |
| 11 | +app = Flask('Discord OAuth2 Example') |
| 12 | +client = discordOauth2(client=159985870458322944, secret=os.environ['oauth2_secret'], |
| 13 | + redirect="https://example.com", token=os.environ['bot_token']) |
| 14 | +#Replace the int above with your application's client ID and the redirect with the redirect URL with the redirect URL this flask hosts. add your oauth2 secret and bot token to a .env file. |
| 15 | +#token could be None. token must be a valid Bot Token or None. Only required if your application adds users to a guild. |
19 | 16 |
|
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" |
| 17 | +@app.route('/') |
| 18 | +def main(): |
| 19 | + return redirect("https://discord.com/api/oauth2/authorize?client_id=159985870458322944&redirect_uri=https%3A%2F%2Fexample.com%2Foauth2&response_type=code&scope=identify%20email%20connections%20guilds%20guilds.join") |
21 | 20 |
|
22 | 21 | @app.route('/oauth2') |
23 | 22 | def oauth(): |
24 | 23 | 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}' |
| 24 | + tokenObject = client.exchange_code(token=code) |
| 25 | + |
| 26 | + identify = tokenObject.identify() |
| 27 | + connections = tokenObject.connections() |
| 28 | + guilds = tokenObject.guilds() |
| 29 | + tokenObject.join_guild(336642139381301249) |
| 30 | + return f"{identify}<br/><br/>{connections}<br/><br/>{guilds}" |
36 | 31 |
|
37 | 32 | def run(): |
38 | 33 | app.run(host="0.0.0.0", port=8080) |
|
0 commit comments