Add YoutubeCredentials model and implement YouTube OAuth login functionality
This commit is contained in:
parent
f7baa91132
commit
4b2f695afb
9 changed files with 315 additions and 1 deletions
37
game/migrations/0008_youtubecredentials.py
Normal file
37
game/migrations/0008_youtubecredentials.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
# Generated by Django 5.2.3 on 2025-06-13 20:16
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("game", "0007_remove_musikgame_music_videos_musicgameorder"),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="YoutubeCredentials",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("credentials", models.JSONField()),
|
||||
(
|
||||
"user",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -3,6 +3,11 @@ from django.db import models
|
|||
from django.urls import reverse
|
||||
|
||||
|
||||
class YoutubeCredentials(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
credentials = models.JSONField()
|
||||
|
||||
|
||||
class Group(models.Model):
|
||||
name = models.CharField(verbose_name="Nom du groupe")
|
||||
owner = models.ForeignKey(
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
<h2>Mes groupes</h2>
|
||||
<p>
|
||||
<a href="{% url "group_create" %}" role="button"><i class="ri-add-box-fill"></i> Créer un groupe</a>
|
||||
{% if not user.youtubecredentials %}
|
||||
<a href="{% url "youtube_login" %}" role="button"><i class="ri-youtube-fill"></i> Me connecter au compte Youtube</a>
|
||||
{% endif %}
|
||||
</p>
|
||||
{% if user.owned_group_set.exists or user.group_set.exists %}
|
||||
<ul>
|
||||
|
|
|
@ -30,4 +30,10 @@ urlpatterns = [
|
|||
"group/<int:pk>/start_game/", views.GameCreateView.as_view(), name="start_game"
|
||||
),
|
||||
path("group/game/<int:pk>/", views.GameDetailView.as_view(), name="game_detail"),
|
||||
path("youtube_login/", views.YoutubeLoginView.as_view(), name="youtube_login"),
|
||||
path(
|
||||
"youtube_callback/",
|
||||
views.YoutubeCallbackView.as_view(),
|
||||
name="youtube_callback",
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import random
|
||||
|
||||
import google_auth_oauthlib
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.db.models import Count, Q
|
||||
from django.http import JsonResponse
|
||||
|
@ -137,3 +139,42 @@ class GameDetailView(LoginRequiredMixin, DetailView):
|
|||
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(group__owner=self.request.user)
|
||||
|
||||
|
||||
class YoutubeLoginView(LoginRequiredMixin, View):
|
||||
def get(self, request):
|
||||
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
||||
settings.YOUTUBE_OAUTH_SECRETS,
|
||||
["https://www.googleapis.com/auth/youtube.force-ssl"],
|
||||
)
|
||||
flow.redirect_uri = "https://localhost/youtube_callback/"
|
||||
auth_url, state = flow.authorization_url(
|
||||
access_type="offline",
|
||||
include_granted_scopes="true",
|
||||
prompt="consent",
|
||||
)
|
||||
self.request.session["state"] = state
|
||||
return redirect(auth_url)
|
||||
|
||||
|
||||
class YoutubeCallbackView(LoginRequiredMixin, View):
|
||||
def get(self, request):
|
||||
if request.GET.get("error"):
|
||||
return redirect("/")
|
||||
print(request.GET)
|
||||
|
||||
state = self.request.session.get("state")
|
||||
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
|
||||
settings.YOUTUBE_OAUTH_SECRETS,
|
||||
["https://www.googleapis.com/auth/youtube.force-ssl"],
|
||||
state=state,
|
||||
)
|
||||
flow.redirect_uri = "https://localhost/youtube_callback/"
|
||||
|
||||
flow.fetch_token(code=request.GET.get("code"))
|
||||
|
||||
credentials = models.YoutubeCredentials(
|
||||
user=request.user, credentials=flow.credentials.to_json()
|
||||
)
|
||||
credentials.save()
|
||||
return redirect("/")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue