Repository: google/google-api-python-client (python)

Issue:
v1.6.1 HttpError 403 "Insufficient Permission"
I happened to try updating google-api-python-client from 1.5.5 to 1.6.1. It causes my YouTube Data API v3 code to always fail with a HttpError. An example to reproduce is:

    from apiclient.discovery import build
    DEVELOPER_KEY = "<Insert key>"
    youtube = build("youtube", "v3", developerKey=DEVELOPER_KEY)
    youtube.videos().list(id="xGG56MWgbOA", part="id").execute()

This returns a successful response on 1.5.5, but raises a HttpError on 1.6.1:

    HttpError: <HttpError 403 when requesting https://www.googleapis.com/youtube/v3/videos?alt=json&part=id&id=xGG56MWgbOA&key=<Insert key> returned "Insufficient Permission">

Visiting the specified link in a browser returns a valid response. Is there some usage change I am not aware of?

Pull request description:
Don't invoke application default credentials logic when developerKey is provided
Resolves #346

Patch:
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
index 6291f34e8..12668832d 100644
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -355,8 +355,9 @@ def build_from_document(
     scopes = list(
       service.get('auth', {}).get('oauth2', {}).get('scopes', {}).keys())
 
-    # If so, then the we need to setup authentication.
-    if scopes:
+    # If so, then the we need to setup authentication if no developerKey is
+    # specified.
+    if scopes and not developerKey:
       # If the user didn't pass in credentials, attempt to acquire application
       # default credentials.
       if credentials is None:


Test patch:
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 47adeb794..ec66bc6d3 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -437,6 +437,16 @@ class DiscoveryFromDocument(unittest.TestCase):
       discovery, base="https://www.googleapis.com/", http=http)
     self.assertEquals(plus._http, http)
 
+  def test_building_with_developer_key_skips_adc(self):
+    discovery = open(datafile('plus.json')).read()
+    plus = build_from_document(
+      discovery, base="https://www.googleapis.com/", developerKey='123')
+    self.assertIsInstance(plus._http, httplib2.Http)
+    # It should not be an AuthorizedHttp, because that would indicate that
+    # application default credentials were used.
+    self.assertNotIsInstance(plus._http, google_auth_httplib2.AuthorizedHttp)
+
+
 class DiscoveryFromHttp(unittest.TestCase):
   def setUp(self):
     self.old_environ = os.environ.copy()