Update YouTube credentials handling to include channel title in home template
This commit is contained in:
parent
aafb7817c2
commit
da29634e57
2 changed files with 11 additions and 1 deletions
|
@ -11,6 +11,8 @@
|
|||
<p>
|
||||
{% if not user.youtubecredentials.credentials %}
|
||||
<a href="{% url "youtube_login" %}" role="button"><i class="ri-youtube-fill"></i> Me connecter au compte Youtube</a>
|
||||
{% else %}
|
||||
<i class="ri-youtube-fill"></i> Connecté au compte Youtube <strong>{{ user.youtubecredentials.credentials.channel_title }}</strong>.
|
||||
{% endif %}
|
||||
</p>
|
||||
{% if user.owned_group_set.exists or user.group_set.exists %}
|
||||
|
|
|
@ -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("/")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue