mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
Model changes
This commit is contained in:
41
flask_discord/_http.py
Normal file
41
flask_discord/_http.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from . import configs
|
||||||
|
|
||||||
|
from flask import session, jsonify
|
||||||
|
from requests_oauthlib import OAuth2Session
|
||||||
|
|
||||||
|
|
||||||
|
class DiscordOAuth2HttpClient(object):
|
||||||
|
|
||||||
|
def __init__(self, client_id, client_secret, redirect_uri):
|
||||||
|
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):
|
||||||
|
session["oauth2_token"] = token
|
||||||
|
|
||||||
|
def _make_session(self, token=None, state=None, scope=None):
|
||||||
|
return OAuth2Session(
|
||||||
|
client_id=self.client_id,
|
||||||
|
token=token,
|
||||||
|
state=state,
|
||||||
|
scope=scope,
|
||||||
|
redirect_uri=self.redirect_uri,
|
||||||
|
auto_refresh_kwargs={
|
||||||
|
'client_id': self.client_id,
|
||||||
|
'client_secret': self.client_secret,
|
||||||
|
},
|
||||||
|
auto_refresh_url=configs.TOKEN_URL,
|
||||||
|
token_updater=self._token_updater)
|
||||||
|
|
||||||
|
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,41 +1,13 @@
|
|||||||
import os
|
from . import configs, _http
|
||||||
|
|
||||||
from . import configs
|
from flask import request, session, redirect
|
||||||
from requests_oauthlib import OAuth2Session
|
|
||||||
|
|
||||||
from flask import request, session, redirect, jsonify
|
|
||||||
|
|
||||||
|
|
||||||
class DiscordOAuth2Session(object):
|
class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
||||||
|
|
||||||
def __init__(self, client_id, client_secret, redirect_uri):
|
|
||||||
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):
|
|
||||||
session["oauth2_token"] = token
|
|
||||||
|
|
||||||
def __make_session(self, token=None, state=None, scope=None):
|
|
||||||
return OAuth2Session(
|
|
||||||
client_id=self.client_id,
|
|
||||||
token=token,
|
|
||||||
state=state,
|
|
||||||
scope=scope,
|
|
||||||
redirect_uri=self.redirect_uri,
|
|
||||||
auto_refresh_kwargs={
|
|
||||||
'client_id': self.client_id,
|
|
||||||
'client_secret': self.client_secret,
|
|
||||||
},
|
|
||||||
auto_refresh_url=configs.TOKEN_URL,
|
|
||||||
token_updater=self.__token_updater)
|
|
||||||
|
|
||||||
def make_session(self):
|
def make_session(self):
|
||||||
scope = request.args.get("scope", str()).split() or configs.DEFAULT_SCOPES
|
scope = request.args.get("scope", str()).split() or configs.DEFAULT_SCOPES
|
||||||
discord_session = self.__make_session(scope=scope)
|
discord_session = self._make_session(scope=scope)
|
||||||
authorization_url, state = discord_session.authorization_url(configs.AUTHORIZATION_BASE_URL)
|
authorization_url, state = discord_session.authorization_url(configs.AUTHORIZATION_BASE_URL)
|
||||||
session["oauth2_state"] = state
|
session["oauth2_state"] = state
|
||||||
return redirect(authorization_url)
|
return redirect(authorization_url)
|
||||||
@@ -43,17 +15,10 @@ class DiscordOAuth2Session(object):
|
|||||||
def callback(self):
|
def callback(self):
|
||||||
if request.values.get("error"):
|
if request.values.get("error"):
|
||||||
return request.values["error"]
|
return request.values["error"]
|
||||||
discord = self.__make_session(state=session.get("oauth2_state"))
|
discord = self._make_session(state=session.get("oauth2_state"))
|
||||||
token = discord.fetch_token(
|
token = discord.fetch_token(
|
||||||
configs.TOKEN_URL,
|
configs.TOKEN_URL,
|
||||||
client_secret=self.client_secret,
|
client_secret=self.client_secret,
|
||||||
authorization_response=request.url
|
authorization_response=request.url
|
||||||
)
|
)
|
||||||
session["oauth2_token"] = token
|
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user