|
62 | 62 | except ImportError: |
63 | 63 | from oauth2client import _helpers as util |
64 | 64 |
|
| 65 | +from googleapiclient import _auth |
65 | 66 | from googleapiclient import mimeparse |
66 | 67 | from googleapiclient.errors import BatchError |
67 | 68 | from googleapiclient.errors import HttpError |
@@ -1126,21 +1127,25 @@ def _refresh_and_apply_credentials(self, request, http): |
1126 | 1127 | # If there is no http per the request then refresh the http passed in |
1127 | 1128 | # via execute() |
1128 | 1129 | creds = None |
1129 | | - if request.http is not None and hasattr(request.http.request, |
1130 | | - 'credentials'): |
1131 | | - creds = request.http.request.credentials |
1132 | | - elif http is not None and hasattr(http.request, 'credentials'): |
1133 | | - creds = http.request.credentials |
| 1130 | + request_credentials = False |
| 1131 | + |
| 1132 | + if request.http is not None: |
| 1133 | + creds = _auth.get_credentials_from_http(request.http) |
| 1134 | + request_credentials = True |
| 1135 | + |
| 1136 | + if creds is None and http is not None: |
| 1137 | + creds = _auth.get_credentials_from_http(http) |
| 1138 | + |
1134 | 1139 | if creds is not None: |
1135 | 1140 | if id(creds) not in self._refreshed_credentials: |
1136 | | - creds.refresh(http) |
| 1141 | + _auth.refresh_credentials(creds) |
1137 | 1142 | self._refreshed_credentials[id(creds)] = 1 |
1138 | 1143 |
|
1139 | 1144 | # Only apply the credentials if we are using the http object passed in, |
1140 | 1145 | # otherwise apply() will get called during _serialize_request(). |
1141 | | - if request.http is None or not hasattr(request.http.request, |
1142 | | - 'credentials'): |
1143 | | - creds.apply(request.headers) |
| 1146 | + if request.http is None or not request_credentials: |
| 1147 | + _auth.apply_credentials(creds, request.headers) |
| 1148 | + |
1144 | 1149 |
|
1145 | 1150 | def _id_to_header(self, id_): |
1146 | 1151 | """Convert an id to a Content-ID header value. |
@@ -1200,9 +1205,10 @@ def _serialize_request(self, request): |
1200 | 1205 | msg = MIMENonMultipart(major, minor) |
1201 | 1206 | headers = request.headers.copy() |
1202 | 1207 |
|
1203 | | - if request.http is not None and hasattr(request.http.request, |
1204 | | - 'credentials'): |
1205 | | - request.http.request.credentials.apply(headers) |
| 1208 | + if request.http is not None: |
| 1209 | + credentials = _auth.get_credentials_from_http(request.http) |
| 1210 | + if credentials is not None: |
| 1211 | + _auth.apply_credentials(credentials, headers) |
1206 | 1212 |
|
1207 | 1213 | # MIMENonMultipart adds its own Content-Type header. |
1208 | 1214 | if 'content-type' in headers: |
@@ -1409,11 +1415,11 @@ def execute(self, http=None): |
1409 | 1415 |
|
1410 | 1416 | # Special case for OAuth2Credentials-style objects which have not yet been |
1411 | 1417 | # refreshed with an initial access_token. |
1412 | | - if getattr(http.request, 'credentials', None) is not None: |
1413 | | - creds = http.request.credentials |
1414 | | - if not getattr(creds, 'access_token', None): |
| 1418 | + creds = _auth.get_credentials_from_http(http) |
| 1419 | + if creds is not None: |
| 1420 | + if not _auth.is_valid(creds): |
1415 | 1421 | LOGGER.info('Attempting refresh to obtain initial access_token') |
1416 | | - creds.refresh(http) |
| 1422 | + _auth.refresh_credentials(creds) |
1417 | 1423 |
|
1418 | 1424 | self._execute(http, self._order, self._requests) |
1419 | 1425 |
|
|
0 commit comments