Skip to content

Commit

Permalink
Merge pull request #24 from iterative/fix-308-code-redirects
Browse files Browse the repository at this point in the history
fix: Redirected but the response is missing a Location: issue on large uploads
  • Loading branch information
shcheklein authored Apr 4, 2020
2 parents c2609b7 + 18f2057 commit 8832350
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pydrive2/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def Refresh(self):
"Please set access_type of OAuth to offline."
)
if self.http is None:
self.http = httplib2.Http(timeout=self.http_timeout)
self.http = self._build_http()
try:
self.credentials.refresh(self.http)
except AccessTokenRefreshError as error:
Expand Down Expand Up @@ -579,6 +579,16 @@ def Authenticate(self, code):
raise AuthenticationError("OAuth2 code exchange failed: %s" % e)
print("Authentication successful.")

def _build_http(self):
http = httplib2.Http(timeout=self.http_timeout)
# 308's are used by several Google APIs (Drive, YouTube)
# for Resumable Uploads rather than Permanent Redirects.
# This asks httplib2 to exclude 308s from the status codes
# it treats as redirects
# See also: https://stackoverflow.com/a/59850170/298182
http.redirect_codes = http.redirect_codes - {308}
return http

def Authorize(self):
"""Authorizes and builds service.
Expand All @@ -590,7 +600,7 @@ def Authorize(self):
)

if self.http is None:
self.http = httplib2.Http(timeout=self.http_timeout)
self.http = self._build_http()
self.http = self.credentials.authorize(self.http)
self.service = build(
"drive", "v2", http=self.http, cache_discovery=False
Expand All @@ -602,6 +612,6 @@ def Get_Http_Object(self):
:return: The http object to be used in each call.
:rtype: httplib2.Http
"""
http = httplib2.Http(timeout=self.http_timeout)
http = self._build_http()
http = self.credentials.authorize(http)
return http

0 comments on commit 8832350

Please sign in to comment.