Skip to content

Commit fa1d4b0

Browse files
authored
Update README.md
1 parent 64eb9f8 commit fa1d4b0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
11
# DiscordOAuth2.py
22
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

Comments
 (0)