diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index 5f160485c4..eb01be65ae 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -1050,6 +1050,9 @@ def createMethod(methodName, methodDesc, rootDesc, schema): def method(self, **kwargs): # Don't bother with doc string, it will be over-written by createMethod. + # Validate credentials for the configured universe. + self._validate_credentials() + for name in kwargs: if name not in parameters.argmap: raise TypeError("Got an unexpected keyword argument {}".format(name)) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 8ffed4907e..635d2acd59 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -2428,6 +2428,44 @@ def test_validate_credentials_with_already_validated_credentials(self): # TODO(google-api-python-client/issues/2365): Add test case for fake configured universe and fake credentials' universe. + def test_validate_credentials_before_api_request(self): + fake_universe = "foo.com" + + http = google_auth_httplib2.AuthorizedHttp( + credentials=mock.Mock(universe_domain=universe.DEFAULT_UNIVERSE), + http=build_http(), + ) + discovery = read_datafile("tasks.json") + tasks = build_from_document( + discovery, + http=http, + client_options=google.api_core.client_options.ClientOptions( + universe_domain=universe.DEFAULT_UNIVERSE + ), + ) + + tasklists = tasks.tasklists() + request = tasklists.list() + + # Check that credentials are indeed verified before request. + assert tasklists._validate_credentials() + + http = google_auth_httplib2.AuthorizedHttp( + credentials=mock.Mock(universe_domain=fake_universe), http=build_http() + ) + discovery = read_datafile("tasks.json") + tasks = build_from_document( + discovery, + http=http, + client_options=google.api_core.client_options.ClientOptions( + universe_domain=universe.DEFAULT_UNIVERSE + ), + ) + + # Check that credentials are verified before request. + with self.assertRaises(universe.UniverseMismatchError): + request = tasks.tasklists().list() + if __name__ == "__main__": unittest.main()