Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 401e886

Browse files
author
Jon Wayne Parrott
authored
Warn when google-auth credentials are used but google-auth-httplib2 isn't available (googleapis#443)
1 parent b63a71d commit 401e886

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

googleapiclient/_auth.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
try:
2020
import google.auth
2121
import google.auth.credentials
22-
import google_auth_httplib2
2322
HAS_GOOGLE_AUTH = True
2423
except ImportError: # pragma: NO COVER
2524
HAS_GOOGLE_AUTH = False
2625

26+
try:
27+
import google_auth_httplib2
28+
except ImportError: # pragma: NO COVER
29+
google_auth_httplib2 = None
30+
2731
try:
2832
import oauth2client
2933
import oauth2client.client
@@ -88,6 +92,12 @@ def authorized_http(credentials):
8892

8993
if HAS_GOOGLE_AUTH and isinstance(
9094
credentials, google.auth.credentials.Credentials):
95+
if google_auth_httplib2 is None:
96+
raise ValueError(
97+
'Credentials from google.auth specified, but '
98+
'google-api-python-client is unable to use these credentials '
99+
'unless google-auth-httplib2 is installed. Please install '
100+
'google-auth-httplib2.')
91101
return google_auth_httplib2.AuthorizedHttp(credentials,
92102
http=build_http())
93103
else:

tests/test__auth.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ def test_authorized_http(self):
6868

6969
authorized_http = _auth.authorized_http(credentials)
7070

71-
self.assertIsInstance(authorized_http, google_auth_httplib2.AuthorizedHttp)
71+
self.assertIsInstance(
72+
authorized_http,
73+
google_auth_httplib2.AuthorizedHttp)
7274
self.assertEqual(authorized_http.credentials, credentials)
7375
self.assertIsInstance(authorized_http.http, httplib2.Http)
7476
self.assertIsInstance(authorized_http.http.timeout, int)
@@ -138,3 +140,16 @@ def tearDown(self):
138140
def test_default_credentials(self):
139141
with self.assertRaises(EnvironmentError):
140142
print(_auth.default_credentials())
143+
144+
145+
class TestGoogleAuthWithoutHttplib2(unittest2.TestCase):
146+
def setUp(self):
147+
_auth.google_auth_httplib2 = None
148+
149+
def tearDown(self):
150+
_auth.google_auth_httplib2 = google_auth_httplib2
151+
152+
def test_default_credentials(self):
153+
credentials = mock.Mock(spec=google.auth.credentials.Credentials)
154+
with self.assertRaises(ValueError):
155+
_auth.authorized_http(credentials)

0 commit comments

Comments
 (0)