Skip to content

Commit 20e6135

Browse files
author
Jon Wayne Parrott
authored
Proactively refresh credentials when applying and treat a missing acces_token as invalid. (#469)
Closes #350, Supersedes #434 Note: This change reveals surprising behavior between default credentials and batches. If you allow `googleapiclient.discovery.build` to use default credentials *and* specify different credentials by providing `batch.execut()` with an explicit `http` argument, your individual requests will use the default credentials and *not* the credentials specified to the bathc http. To avoid this, tell `build` explicitly not to use default credentials by specifying `build(..., http=httplib2.Http()`. For context, see: #434 (comment)
1 parent 043fd70 commit 20e6135

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

googleapiclient/_auth.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ def refresh_credentials(credentials):
120120

121121
def apply_credentials(credentials, headers):
122122
# oauth2client and google-auth have the same interface for this.
123+
if not is_valid(credentials):
124+
refresh_credentials(credentials)
123125
return credentials.apply(headers)
124126

125127

@@ -128,7 +130,9 @@ def is_valid(credentials):
128130
credentials, google.auth.credentials.Credentials):
129131
return credentials.valid
130132
else:
131-
return not credentials.access_token_expired
133+
return (
134+
credentials.access_token is not None and
135+
not credentials.access_token_expired)
132136

133137

134138
def get_credentials_from_http(http):

tests/test_http.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def __init__(self, bearer_token, expired=False):
7070
self._bearer_token = bearer_token
7171
self._access_token_expired = expired
7272

73+
@property
74+
def access_token(self):
75+
return self._bearer_token
76+
7377
@property
7478
def access_token_expired(self):
7579
return self._access_token_expired

0 commit comments

Comments
 (0)