mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
tests
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
|
||||
from . import configs
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
||||
from flask import request, session, redirect
|
||||
from flask import request, session, redirect, jsonify
|
||||
|
||||
|
||||
class DiscordOAuth2Session(object):
|
||||
@@ -10,6 +12,8 @@ class DiscordOAuth2Session(object):
|
||||
self.client_id = client_id
|
||||
self.client_secret = client_secret
|
||||
self.redirect_uri = redirect_uri
|
||||
if "http://" in self.redirect_uri:
|
||||
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "true"
|
||||
|
||||
@staticmethod
|
||||
def __token_updater(token):
|
||||
@@ -46,3 +50,10 @@ class DiscordOAuth2Session(object):
|
||||
authorization_response=request.url
|
||||
)
|
||||
session["oauth2_token"] = token
|
||||
|
||||
def get_json(self):
|
||||
discord_session = self.__make_session(token=session.get("oauth2_token"))
|
||||
user = discord_session.get(configs.API_BASE_URL + '/users/@me').json()
|
||||
guilds = discord_session.get(configs.API_BASE_URL + '/users/@me/guilds').json()
|
||||
connections = discord_session.get(configs.API_BASE_URL + '/users/@me/connections').json()
|
||||
return jsonify(user=user, guilds=guilds, connections=connections)
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
from . import configs
|
||||
from requests_oauthlib import OAuth2Session
|
||||
|
||||
from flask import current_app, request, session, sessions, redirect
|
||||
|
||||
|
||||
class Session(dict, sessions.SessionMixin):
|
||||
|
||||
def to_discord(self, scope=str()):
|
||||
scope = (request.args.get("scope") or scope).split()
|
||||
discord_session = OAuth2Session()
|
||||
|
||||
def __init__(self, client_id, client_secret, redirect_uri, token_updater=None):
|
||||
auto_refresh_kwargs = {
|
||||
"client_id": client_id,
|
||||
"client_secret": client_secret,
|
||||
}
|
||||
super().__init__(
|
||||
client_id=client_id, redirect_uri=redirect_uri,
|
||||
auto_refresh_kwargs=auto_refresh_kwargs,
|
||||
auto_refresh_url=configs.TOKEN_URL, token_updater=token_updater
|
||||
)
|
||||
|
||||
def create_session(self, scope=None):
|
||||
self.scope = scope
|
||||
authorization_url, state = self.authorization_url(configs.AUTHORIZATION_BASE_URL)
|
||||
session["oauth2_state"] = state
|
||||
return redirect(authorization_url)
|
||||
|
||||
|
||||
class MySessionInterface(sessions.SessionInterface):
|
||||
|
||||
def __init__(self, client_id, client_secret, redirect_uri, token_updater=None):
|
||||
pass
|
||||
|
||||
def open_session(self, app, request):
|
||||
pass
|
||||
|
||||
def save_session(self, app, session, response):
|
||||
|
||||
|
||||
|
||||
class DiscordOAuth2Session(object):
|
||||
|
||||
def __init__(self, app):
|
||||
app.session_interface = Session()
|
||||
31
tests/test_app.py
Normal file
31
tests/test_app.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from flask import Flask, redirect, url_for
|
||||
from flask_discord import DiscordOAuth2Session
|
||||
|
||||
OAUTH2_CLIENT_ID = 490732332240863233
|
||||
OAUTH2_CLIENT_SECRET = "GjKMenfebgLrOYQ_A_X7ouaWv9IhWdbI"
|
||||
OAUTH2_REDIRECT_URI = "http://127.0.0.1:5000/callback"
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = b"%\xe0'\x01\xdeH\x8e\x85m|\xb3\xffCN\xc9g"
|
||||
discord = DiscordOAuth2Session(OAUTH2_CLIENT_ID, OAUTH2_CLIENT_SECRET, OAUTH2_REDIRECT_URI)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return discord.make_session()
|
||||
|
||||
|
||||
@app.route("/callback")
|
||||
def callback():
|
||||
discord.callback()
|
||||
return redirect(url_for(".me"))
|
||||
|
||||
|
||||
@app.route("/me")
|
||||
def me():
|
||||
return discord.get_json()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
Reference in New Issue
Block a user