+
+
+@login_required
+def oauth2(request):
+ redirect_uri = request.build_absolute_uri(reverse("apiclient_oauth2_redirect"))
+ oauth = OAuth2Session(YOUTUBE_CLIENT_ID, redirect_uri=redirect_uri, scope=YOUTUBE_SCOPE)
+ authorization_url, state = oauth.authorization_url(YOUTUBE_AUTH_URL)
+ return HttpResponseRedirect(authorization_url)
+
+
+@login_required
+def oauth2_redirect(request):
+ redirect_uri = request.build_absolute_uri(reverse("apiclient_oauth2_redirect"))
+ oauth = OAuth2Session(YOUTUBE_CLIENT_ID, redirect_uri=redirect_uri, scope=YOUTUBE_SCOPE)
+ token = oauth.fetch_token(
+ YOUTUBE_TOKEN_URL,
+ authorization_response=request.get_full_path(),
+ client_secret=YOUTUBE_CLIENT_SECRET,
+ )
+ YouTubeToken.objects.all().delete()
+ YouTubeToken.objects.create(token=json.dumps(token))
+ return HttpResponseRedirect('/')