From da29634e579d32c797adbe56398ddb5d7d1a1dee Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sun, 15 Jun 2025 10:17:08 +0200 Subject: [PATCH] Update YouTube credentials handling to include channel title in home template --- game/templates/game/home.html | 2 ++ game/views.py | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/game/templates/game/home.html b/game/templates/game/home.html index fe97624..eb7e220 100644 --- a/game/templates/game/home.html +++ b/game/templates/game/home.html @@ -11,6 +11,8 @@

{% if not user.youtubecredentials.credentials %} Me connecter au compte Youtube + {% else %} + Connecté au compte Youtube {{ user.youtubecredentials.credentials.channel_title }}. {% endif %}

{% if user.owned_group_set.exists or user.group_set.exists %} diff --git a/game/views.py b/game/views.py index cec8875..cbcbdb8 100644 --- a/game/views.py +++ b/game/views.py @@ -1,6 +1,7 @@ import random import google_auth_oauthlib +import googleapiclient from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin @@ -363,6 +364,13 @@ class YoutubeCallbackView(LoginRequiredMixin, View): flow.fetch_token(code=request.GET.get("code")) credentials = flow.credentials + + yt_api = googleapiclient.discovery.build( + "youtube", "v3", credentials=credentials + ) + channel_request = yt_api.channels().list(part="snippet", mine=True) + res = channel_request.execute() + models.YoutubeCredentials.objects.update_or_create( user=request.user, defaults={ @@ -373,10 +381,10 @@ class YoutubeCallbackView(LoginRequiredMixin, View): "client_id": credentials.client_id, "client_secret": credentials.client_secret, "granted_scopes": credentials.granted_scopes, + "channel_title": res["items"][0]["snippet"]["title"], } }, ) - messages.add_message(request, messages.SUCCESS, "Connexion à Youtube réussie.") return redirect("/")