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
|
@ -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