|
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 | 3 |
|
4 | | -> **Notice:** I don't know how to make my library work with `pip install` so you'll have to copy the source code and paste it into your workspace. |
5 | | -
|
6 | | -### Using DiscordOAuth2.py with Flask |
7 | | -You can try out a working example here: https://DiscordOAuth2py.treeben77.repl.co |
8 | | -```python |
9 | | -from flask import Flask, redirect, request |
10 | | -from discordoauth2 import discordOauth2 |
11 | | -import os |
12 | | - |
13 | | -app = Flask('Discord OAuth2 Example') |
14 | | -client = discordOauth2(client=your-client-id, secret="your-client-secret", |
15 | | -redirect="your-redirect-url", token="your-bot-token (optional)") |
16 | | -# Replace your-client-id with your application's client id, replace your-client-secret with your client secret and replace your-redirect-url with the url that discord will redirect users to once they complete OAuth2. |
17 | | -# If you want to add users to a guild, insert a bot token with CREATE_INSTANT_INVITE permissions in the guilds you want to add users to. |
18 | | - |
19 | | -@app.route('/') |
20 | | -def main(): |
21 | | - # Your OAuth2 url, you can make one a https://discord.dev |
22 | | - return redirect("https://discord.com/api/oauth2/authorize") |
23 | | - |
24 | | -@app.route('/oauth2') |
25 | | -def oauth(): |
26 | | - tokenObject = client.exchange_code(token=request.args.get('code')) |
27 | | - print("refresh token: "+tokenObject.refresh_token) |
28 | | - |
29 | | - # returns basic data about the user, including username, avatar and badges, if the email scope was parsed, it will also return their email. |
30 | | - identify = tokenObject.access.identify() |
31 | | - # returns visible and hidden connections such as GitHub, YouTube or Twitter. |
32 | | - connections = tokenObject.access.connections() |
33 | | - # returns a list of guilds that the user is in |
34 | | - guilds = tokenObject.access.guilds() |
35 | | - # returns a member object for the provided guild |
36 | | - guilds_member = tokenObject.access.guilds_member(guilds[0]["id"]) |
37 | | - # makes a user join a guild, bot token provided must have CREATE_INSTANT_INVITE in that guild |
38 | | - tokenObject.access.guilds_join(guild-id-here) |
39 | | - |
40 | | - return f"{identify}<br><br>{connections}<br><br>{guilds}<br><br>guild data for the first guild: {guilds_member}" |
41 | | - |
42 | | -app.run(host="0.0.0.0", port=8080) |
| 4 | +### Useful Links |
| 5 | +Discord Server: https://discord.gg/DJ9xbbZAP5 |
| 6 | + |
| 7 | +Documentation is coming soon, don't worry. |
| 8 | + |
| 9 | +## Quickstart |
| 10 | +### Installing |
| 11 | +I've finally published the library to PyPi! So now you can use pip. |
| 12 | +``` |
| 13 | +pip install discord-oauth2.py |
| 14 | +``` |
| 15 | +### Example With Flask |
| 16 | +Don't forget to replace all the client information on line 20 and 21 with your application's own information. You can leave bot token empty if your not adding members to guilds. |
| 17 | +```py |
| 18 | +import discordoauth2 |
| 19 | +from flask import Flask, request |
| 20 | + |
| 21 | +client = discordoauth2.Client(849930878276993044, secret="very-secret-code", |
| 22 | +redirect="https://findingfakeurlsisprettyhard.tv/oauth2", bot_token="bot-token-only-required-for-guild-joining") |
| 23 | +app = Flask(__name__) |
| 24 | + |
| 25 | +@app.route("/oauth2") |
| 26 | +def oauth2(): |
| 27 | + code = request.args.get("code") |
| 28 | + |
| 29 | + access = client.exchange_code(code) |
| 30 | + |
| 31 | + identify = access.fetch_identify() |
| 32 | + connections = access.fetch_connections() |
| 33 | + guilds = access.fetch_guilds() |
| 34 | + |
| 35 | + return f"""{identify}<br><br>{connections}<br><br>{guilds}""" |
| 36 | + |
| 37 | +app.run("0.0.0.0", 8080) |
43 | 38 | ``` |
0 commit comments