Skip to content

Commit

Permalink
Update to support new changes to discordOAuth2.py
Browse files Browse the repository at this point in the history
  • Loading branch information
treeben77 committed Jul 4, 2021
1 parent 4ed7114 commit 209fba5
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@
Use Discord's OAuth2 effortlessly! Turns the auth code to a access token and the access token into scope infomation.

### Using DiscordOAut2.py with Flask

```python
from flask import Flask, redirect, request
from threading import Thread
import discordoauth2 as oauth2
from discordoauth2 import discordOauth2
import os

app = Flask('')
oauth2.client({
'client_id': '849<example>993044', #the client id for your application
'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.
'redirect_uri': 'https://example.com/oauth2', #the redirect for the uri below
'bot_token': os.environ['bot_token'] #the token for your app's bot, only required if you need to use guild.join
})
app = Flask('Discord OAuth2 Example')
client = discordOauth2(client=159985870458322944, secret=os.environ['oauth2_secret'],
redirect="https://example.com", token=os.environ['bot_token'])
#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.
#token could be None. token must be a valid Bot Token or None. Only required if your application adds users to a guild.

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"
@app.route('/')
def main():
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")

@app.route('/oauth2')
def oauth():
code = request.args.get('code')
if not code:
return redirect(oauth2_uri)
else:
token = oauth2.exchange_code(code)
if str(token) == """{'error': 'invalid_request', 'error_description': 'Invalid "code" in request.'}""":
return redirect(oauth2_uri)
identify = oauth2.get_identify(token['access_token'])
connections = oauth2.get_connections(token['access_token'])
guilds = oauth2.get_guilds(token['access_token'])
oauth2.join_guild(token['access_token'], 849912914450448395)
return f'{identify}<br/><br/>{connections}<br/><br/>{guilds}'
tokenObject = client.exchange_code(token=code)

identify = tokenObject.identify()
connections = tokenObject.connections()
guilds = tokenObject.guilds()
tokenObject.join_guild(336642139381301249)
return f"{identify}<br/><br/>{connections}<br/><br/>{guilds}"

def run():
app.run(host="0.0.0.0", port=8080)
Expand Down

0 comments on commit 209fba5

Please sign in to comment.