Skip to content

Commit c1ecfbe

Browse files
authored
Update README.md
1 parent e26d5b9 commit c1ecfbe

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,40 @@
22
Use Discord's OAuth2 effortlessly! Turns the auth code to a access token and the access token into scope infomation.
33

44
### Using DiscordOAuth2.py with Flask
5+
You can try out a working example here: https://DiscordOAuth2py.treeben77.repl.co
56
```python
67
from flask import Flask, redirect, request
7-
from threading import Thread
88
from discordoauth2 import discordOauth2
99
import os
1010

1111
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.
12+
client = discordOauth2(client=your-client-id, secret="your-client-secret",
13+
redirect="your-redirect-url", token="your-bot-token (optional)")
14+
# 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.
15+
# 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.
1616

1717
@app.route('/')
1818
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")
19+
# Your OAuth2 url, you can make one a https://discord.dev
20+
return redirect("https://discord.com/api/oauth2/authorize")
2021

2122
@app.route('/oauth2')
2223
def oauth():
23-
code = request.args.get('code')
24-
tokenObject = client.exchange_code(token=code)
24+
tokenObject = client.exchange_code(token=request.args.get('code'))
25+
print("refresh token: "+tokenObject.refresh_token)
2526

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}"
27+
# returns basic data about the user, including username, avatar and badges, if the email scope was parsed, it will also return their email.
28+
identify = tokenObject.access.identify()
29+
# returns visible and hidden connections such as GitHub, YouTube or Twitter.
30+
connections = tokenObject.access.connections()
31+
# returns a list of guilds that the user is in
32+
guilds = tokenObject.access.guilds()
33+
# returns a member object for the provided guild
34+
guilds_member = tokenObject.access.guilds_member(guilds[0]["id"])
35+
# makes a user join a guild, bot token provided must have CREATE_INSTANT_INVITE in that guild
36+
tokenObject.access.guilds_join(guild-id-here)
3137

32-
def run():
33-
app.run(host="0.0.0.0", port=8080)
38+
return f"{identify}<br><br>{connections}<br><br>{guilds}<br><br>guild data for the first guild: {guilds_member}"
3439

35-
server = Thread(target=run)
36-
server.start()
40+
app.run(host="0.0.0.0", port=8080)
3741
```

0 commit comments

Comments
 (0)