我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用oauth2client.client.HttpAccessTokenRefreshError()。
def _retry_api_check(exception): """Return True if we should retry. False otherwise. Args: exception: An exception to test for transience. Returns: True if we should retry. False otherwise. """ _print_error('Exception %s: %s' % (type(exception).__name__, str(exception))) if isinstance(exception, apiclient.errors.HttpError): if exception.resp.status in TRANSIENT_HTTP_ERROR_CODES: return True if isinstance(exception, socket.error): if exception.errno in TRANSIENT_SOCKET_ERROR_CODES: return True if isinstance(exception, HttpAccessTokenRefreshError): return True return False
def _refresh(self, http): """Refreshes the access token. Skip all the storage hoops and just refresh using the API. Args: http: an object to be used to make HTTP requests. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ try: self._retrieve_info(http) self.access_token, self.token_expiry = _metadata.get_token( http, service_account=self.service_account_email) except http_client.HTTPException as err: raise client.HttpAccessTokenRefreshError(str(err))
def _refresh(self, http_request): """Refreshes the access_token. Skip all the storage hoops and just refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ try: self._retrieve_info(http_request) self.access_token, self.token_expiry = _metadata.get_token( http_request, service_account=self.service_account_email) except httplib2.HttpLib2Error as e: raise HttpAccessTokenRefreshError(str(e))
def get_user_credentials_object(self): if not self.credentials: cr_json = self.get_google_credentials() if cr_json: # Note JSON is stored as escaped string, not dict cr = client.Credentials.new_from_json(cr_json) expires_in = cr.token_expiry - datetime.utcnow() logging.debug("expires_in: %s" % expires_in) if expires_in < timedelta(minutes=15): try: cr.refresh(httplib2.Http()) except client.HttpAccessTokenRefreshError, e: logging.error("HttpAccessTokenRefreshError: %s" % e) cr = None else: self.set_google_credentials(cr) self.credentials = cr return cr
def _refresh(self, http_request): """Refreshes the access_token. Skip all the storage hoops and just refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ query = '?scope=%s' % urllib.parse.quote(self.scope, '') uri = META.replace('{?scope}', query) response, content = http_request( uri, headers={'Metadata-Flavor': 'Google'}) content = _from_bytes(content) if response.status == http_client.OK: try: token_content = json.loads(content) except Exception as e: raise HttpAccessTokenRefreshError(str(e), status=response.status) self.access_token = token_content['access_token'] else: if response.status == http_client.NOT_FOUND: content += (' This can occur if a VM was created' ' with no service account or scopes.') raise HttpAccessTokenRefreshError(content, status=response.status)
def main(): flags = parse_cmdline() logger = configure_logs(flags.logfile) pageTokenFile = PageTokenFile(flags.ptokenfile) for i in range(RETRY_NUM): try: service = build_service(flags) pageToken = pageTokenFile.get() deletionList, pageTokenBefore, pageTokenAfter = \ get_deletion_list(service, pageToken, flags) pageTokenFile.save(pageTokenBefore) listEmpty = delete_old_files(service, deletionList, flags) except client.HttpAccessTokenRefreshError: print('Authentication error') except httplib2.ServerNotFoundError as e: print('Error:', e) except TimeoutError: print('Timeout: Google backend error.') print('Retries unsuccessful. Abort action.') return else: break time.sleep(RETRY_INTERVAL) else: print("Retries unsuccessful. Abort action.") return if listEmpty: pageTokenFile.save(pageTokenAfter)
def test_token_refresh_failure(self): for status_code in client.REFRESH_STATUS_CODES: http = http_mock.HttpMockSequence([ ({'status': status_code}, b''), ({'status': http_client.BAD_REQUEST}, b'{"error":"access_denied"}'), ]) http = self.credentials.authorize(http) with self.assertRaises( client.HttpAccessTokenRefreshError) as exc_manager: transport.request(http, 'http://example.com') self.assertEqual(http_client.BAD_REQUEST, exc_manager.exception.status) self.assertTrue(self.credentials.access_token_expired) self.assertEqual(None, self.credentials.token_response)
def _do_refresh_request_test_helper(self, response, content, error_msg, logger, gen_body, gen_headers, store=None): token_uri = 'http://token_uri' credentials = client.OAuth2Credentials(None, None, None, None, None, token_uri, None) credentials.store = store http = http_mock.HttpMock(headers=response, data=content) with self.assertRaises( client.HttpAccessTokenRefreshError) as exc_manager: credentials._do_refresh_request(http) self.assertEqual(exc_manager.exception.args, (error_msg,)) self.assertEqual(exc_manager.exception.status, response.status) # Verify mocks. self.assertEqual(http.requests, 1) self.assertEqual(http.uri, token_uri) self.assertEqual(http.method, 'POST') self.assertEqual(http.body, gen_body.return_value) self.assertEqual(http.headers, gen_headers.return_value) call1 = mock.call('Refreshing access_token') failure_template = 'Failed to retrieve access token: %s' call2 = mock.call(failure_template, content) self.assertEqual(logger.info.mock_calls, [call1, call2]) if store is not None: store.locked_put.assert_called_once_with(credentials)
def _refresh(self, http_request): """Refreshes the access_token. Skip all the storage hoops and just refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ response, content = http_request( META, headers={'Metadata-Flavor': 'Google'}) content = _from_bytes(content) if response.status == http_client.OK: try: token_content = json.loads(content) except Exception as e: raise HttpAccessTokenRefreshError(str(e), status=response.status) self.access_token = token_content['access_token'] else: if response.status == http_client.NOT_FOUND: content += (' This can occur if a VM was created' ' with no service account or scopes.') raise HttpAccessTokenRefreshError(content, status=response.status)
def _refresh(self, http_request): """Refreshes the access_token. Skip all the storage hoops and just refresh using the API. Args: http_request: callable, a callable that matches the method signature of httplib2.Http.request, used to make the refresh request. Raises: HttpAccessTokenRefreshError: When the refresh fails. """ query = '?scope=%s' % urllib.parse.quote(self.scope, '') uri = META.replace('{?scope}', query) response, content = http_request(uri) content = _from_bytes(content) if response.status == 200: try: d = json.loads(content) except Exception as e: raise HttpAccessTokenRefreshError(str(e), status=response.status) self.access_token = d['accessToken'] else: if response.status == 404: content += (' This can occur if a VM was created' ' with no service account or scopes.') raise HttpAccessTokenRefreshError(content, status=response.status)
def __init__(self, *args, **kwargs): super(GCP, self).__init__(*args, **kwargs) self._zones = [] self._project = 'fusion-gce-testing' if str(self._profile_name).lower() == 'old'\ else CONFIG.GCP_PROJECT_PREFIX + str(self._profile_name).lower() if self._profile_name != 'default': credentials_file = os.path.dirname(__file__) + '/' + str(self._profile_name).upper() + '.json' if os.path.isfile(credentials_file): os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_file else: print('Credentials file %s does not exist.' % credentials_file) exit(1) credentials = GoogleCredentials.get_application_default() self._compute = discovery.build('compute', 'v1', credentials=credentials) zones = None try: zones = self._compute.zones().list(project=self._project).execute() except HttpAccessTokenRefreshError as e: print('Auth Error (%s)' % e) exit(1) for zone in zones['items']: self._zones.append(zone['name']) region = str(zone['name']).rsplit('-', 1)[0] if region not in self._regions: self._regions.append(region) self._check_region()