Warn when google-auth credentials are used but google-auth-httplib2 isn't available (#443)
diff --git a/googleapiclient/_auth.py b/googleapiclient/_auth.py
index 92f2796..ada4ffb 100644
--- a/googleapiclient/_auth.py
+++ b/googleapiclient/_auth.py
@@ -19,12 +19,16 @@
try:
import google.auth
import google.auth.credentials
- import google_auth_httplib2
HAS_GOOGLE_AUTH = True
except ImportError: # pragma: NO COVER
HAS_GOOGLE_AUTH = False
try:
+ import google_auth_httplib2
+except ImportError: # pragma: NO COVER
+ google_auth_httplib2 = None
+
+try:
import oauth2client
import oauth2client.client
HAS_OAUTH2CLIENT = True
@@ -88,6 +92,12 @@
if HAS_GOOGLE_AUTH and isinstance(
credentials, google.auth.credentials.Credentials):
+ if google_auth_httplib2 is None:
+ raise ValueError(
+ 'Credentials from google.auth specified, but '
+ 'google-api-python-client is unable to use these credentials '
+ 'unless google-auth-httplib2 is installed. Please install '
+ 'google-auth-httplib2.')
return google_auth_httplib2.AuthorizedHttp(credentials,
http=build_http())
else:
diff --git a/tests/test__auth.py b/tests/test__auth.py
index 68e7aae..003098e 100644
--- a/tests/test__auth.py
+++ b/tests/test__auth.py
@@ -68,7 +68,9 @@
authorized_http = _auth.authorized_http(credentials)
- self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp)
+ self.assertIsInstance(
+ authorized_http,
+ google_auth_httplib2.AuthorizedHttp)
self.assertEqual(authorized_http.credentials, credentials)
self.assertIsInstance(authorized_http.http, httplib2.Http)
self.assertIsInstance(authorized_http.http.timeout, int)
@@ -138,3 +140,16 @@
def test_default_credentials(self):
with self.assertRaises(EnvironmentError):
print(_auth.default_credentials())
+
+
+class TestGoogleAuthWithoutHttplib2(unittest2.TestCase):
+ def setUp(self):
+ _auth.google_auth_httplib2 = None
+
+ def tearDown(self):
+ _auth.google_auth_httplib2 = google_auth_httplib2
+
+ def test_default_credentials(self):
+ credentials = mock.Mock(spec=google.auth.credentials.Credentials)
+ with self.assertRaises(ValueError):
+ _auth.authorized_http(credentials)