Skip to content

Commit

Permalink
Fix error with discord callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwizi committed May 6, 2024
1 parent d2b4cf8 commit 36430d1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
72 changes: 36 additions & 36 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cs2-battle-bot-api"
version = "0.0.34"
version = "0.0.35"
description = ""
authors = ["Adrian Ciolek <[email protected]>"]
readme = "README.md"
Expand Down
8 changes: 5 additions & 3 deletions src/accounts/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ def __init__(self) -> None:
self.token_url = "https://discord.com/api/oauth2/token"

def get_login_url(self, request):
redirect_url = request.build_absolute_uri(reverse_lazy("discord_callback"))
redirect_url = request.build_absolute_uri(reverse("discord_callback"))
print(redirect_url)
return (f""
f"{self.auth_url}?client_id={settings.DISCORD_CLIENT_ID}"
f"&response_type=code"
f"&redirect_uri={redirect_url}"
f"&scope=identify+email")

def exchange_code(self, code: str) -> dict:
def exchange_code(self, code: str, request) -> dict:
"""
Exchange a code for an access token.
Expand All @@ -194,10 +195,11 @@ def exchange_code(self, code: str) -> dict:
-------
dict: The access token.
"""
redirect_url = request.build_absolute_uri(reverse("discord_callback"))
data = {
"grant_type": "authorization_code",
"code": code,
"redirect_uri": settings.DISCORD_REDIRECT_URI,
"redirect_uri": redirect_url,
}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
with httpx.Client() as client:
Expand Down
2 changes: 1 addition & 1 deletion src/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def discord_callback(request):

discord_auth = DiscordAuthService()
try:
token = discord_auth.exchange_code(code)
token = discord_auth.exchange_code(code, request=request)
except httpx.HTTPError as e:
print(e)
return JsonResponse({"error": repr(e)}, status=400)
Expand Down

0 comments on commit 36430d1

Please sign in to comment.