From 6a30b55d0fd4976571a5b9b34c01fd41cec49c7a Mon Sep 17 00:00:00 2001 From: kootsoop Date: Thu, 7 May 2020 09:05:53 -0400 Subject: [PATCH] Spacing changed. Added 308 redirect change to fix #293 --- youtube_upload/auth/__init__.py | 16 ++++++++++------ youtube_upload/upload_video.py | 14 ++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/youtube_upload/auth/__init__.py b/youtube_upload/auth/__init__.py index 71665cb..da7e7a9 100644 --- a/youtube_upload/auth/__init__.py +++ b/youtube_upload/auth/__init__.py @@ -1,17 +1,17 @@ """Wrapper for Google OAuth2 API.""" -import sys -import json import googleapiclient.discovery +import httplib2 import oauth2client -import httplib2 -from youtube_upload import lib -from youtube_upload.auth import console from youtube_upload.auth import browser +from youtube_upload.auth import console +from oauth2client import client +from oauth2client import file YOUTUBE_UPLOAD_SCOPE = ["https://www.googleapis.com/auth/youtube.upload", "https://www.googleapis.com/auth/youtube"] + def _get_credentials_interactively(flow, storage, get_code_callback): """Return the credentials asking the user.""" flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN @@ -23,6 +23,7 @@ def _get_credentials_interactively(flow, storage, get_code_callback): credential.set_store(storage) return credential + def _get_credentials(flow, storage, get_code_callback): """Return the user credentials. If not found, run the interactive flow.""" existing_credentials = storage.get() @@ -31,6 +32,7 @@ def _get_credentials(flow, storage, get_code_callback): else: return _get_credentials_interactively(flow, storage, get_code_callback) + def get_resource(client_secrets_file, credentials_file, get_code_callback): """Authenticate and return a googleapiclient.discovery.Resource object.""" get_flow = oauth2client.client.flow_from_clientsecrets @@ -38,5 +40,7 @@ def get_resource(client_secrets_file, credentials_file, get_code_callback): storage = oauth2client.file.Storage(credentials_file) credentials = _get_credentials(flow, storage, get_code_callback) if credentials: - http = credentials.authorize(httplib2.Http()) + httplib = httplib2.Http() + httplib.redirect_codes = httplib.redirect_codes - {308} + http = credentials.authorize(httplib) return googleapiclient.discovery.build("youtube", "v3", http=http) diff --git a/youtube_upload/upload_video.py b/youtube_upload/upload_video.py index b969594..56267f6 100644 --- a/youtube_upload/upload_video.py +++ b/youtube_upload/upload_video.py @@ -19,6 +19,7 @@ googleapiclient.errors.HttpError, ] + def _upload_to_request(request, progress_callback): """Upload a video to a Youtube request. Return video ID.""" while 1: @@ -31,13 +32,14 @@ def _upload_to_request(request, progress_callback): else: raise KeyError("Expected field 'id' not found in response") -def upload(resource, path, body, chunksize=4*1024*1024, - progress_callback=None, max_retries=10): + +def upload(resource, path, body, chunksize=4 * 1024 * 1024, + progress_callback=None, max_retries=10): """Upload video to Youtube. Return video ID.""" body_keys = ",".join(body.keys()) - media = apiclient.http.MediaFileUpload(path, chunksize=chunksize, - resumable=True, mimetype="application/octet-stream") + media = apiclient.http.MediaFileUpload(path, chunksize=chunksize, + resumable=True, mimetype="application/octet-stream") request = resource.videos().insert(part=body_keys, body=body, media_body=media) upload_fun = lambda: _upload_to_request(request, progress_callback) - return lib.retriable_exceptions(upload_fun, - RETRIABLE_EXCEPTIONS, max_retries=max_retries) + return lib.retriable_exceptions(upload_fun, + RETRIABLE_EXCEPTIONS, max_retries=max_retries)