Skip to content

Commit 6ba1299

Browse files
authored
Merge pull request #716 from AnswerDotAI/erikgaas/fix_discord_client
Fix Discord Client
2 parents 01867d6 + a54498c commit 6ba1299

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

fasthtml/oauth.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class DiscordAppClient(_AppClient):
6969
base_url = "https://discord.com/oauth2/authorize"
7070
token_url = "https://discord.com/api/oauth2/token"
7171
revoke_url = "https://discord.com/api/oauth2/token/revoke"
72+
info_url = "https://discord.com/api/users/@me"
7273
id_key = 'id'
7374

7475
def __init__(self, client_id, client_secret, is_user=False, perms=0, scope=None, **kwargs):
@@ -77,14 +78,18 @@ def __init__(self, client_id, client_secret, is_user=False, perms=0, scope=None,
7778
self.perms = perms
7879
super().__init__(client_id, client_secret, scope=scope, **kwargs)
7980

80-
def login_link(self):
81+
def login_link(self, redirect_uri=None, scope=None, state=None):
82+
use_scope = scope or self.scope
8183
d = dict(response_type='code', client_id=self.client_id,
82-
integration_type=self.integration_type, scope=self.scope) #, permissions=self.perms, prompt='consent')
84+
integration_type=self.integration_type, scope=use_scope)
85+
if state: d['state'] = state
86+
if redirect_uri: d['redirect_uri'] = redirect_uri
8387
return f'{self.base_url}?' + urlencode(d)
8488

85-
def parse_response(self, code):
89+
def parse_response(self, code, redirect_uri=None):
8690
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
87-
data = dict(grant_type='authorization_code', code=code)#, redirect_uri=self.redirect_uri)
91+
data = dict(grant_type='authorization_code', code=code)
92+
if redirect_uri: data['redirect_uri'] = redirect_uri
8893
r = httpx.post(self.token_url, data=data, headers=headers, auth=(self.client_id, self.client_secret))
8994
r.raise_for_status()
9095
self.parse_request_body_response(r.text)

nbs/api/08_oauth.ipynb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
" base_url = \"https://discord.com/oauth2/authorize\"\n",
162162
" token_url = \"https://discord.com/api/oauth2/token\"\n",
163163
" revoke_url = \"https://discord.com/api/oauth2/token/revoke\"\n",
164+
" info_url = \"https://discord.com/api/users/@me\"\n",
164165
" id_key = 'id'\n",
165166
"\n",
166167
" def __init__(self, client_id, client_secret, is_user=False, perms=0, scope=None, **kwargs):\n",
@@ -169,14 +170,18 @@
169170
" self.perms = perms\n",
170171
" super().__init__(client_id, client_secret, scope=scope, **kwargs)\n",
171172
"\n",
172-
" def login_link(self):\n",
173+
" def login_link(self, redirect_uri=None, scope=None, state=None):\n",
174+
" use_scope = scope or self.scope\n",
173175
" d = dict(response_type='code', client_id=self.client_id,\n",
174-
" integration_type=self.integration_type, scope=self.scope) #, permissions=self.perms, prompt='consent')\n",
176+
" integration_type=self.integration_type, scope=use_scope)\n",
177+
" if state: d['state'] = state\n",
178+
" if redirect_uri: d['redirect_uri'] = redirect_uri\n",
175179
" return f'{self.base_url}?' + urlencode(d)\n",
176180
"\n",
177-
" def parse_response(self, code):\n",
181+
" def parse_response(self, code, redirect_uri=None):\n",
178182
" headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
179-
" data = dict(grant_type='authorization_code', code=code)#, redirect_uri=self.redirect_uri)\n",
183+
" data = dict(grant_type='authorization_code', code=code)\n",
184+
" if redirect_uri: data['redirect_uri'] = redirect_uri\n",
180185
" r = httpx.post(self.token_url, data=data, headers=headers, auth=(self.client_id, self.client_secret))\n",
181186
" r.raise_for_status()\n",
182187
" self.parse_request_body_response(r.text)"

0 commit comments

Comments
 (0)