Fix the client to respect the passed in developerKey and credentials (#593)

* Fix googleapiclient to respect the passed in developerKey and credentials

* Fix tests to consider developerKey and credentials
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py
index 7762d84..e86a297 100644
--- a/googleapiclient/discovery.py
+++ b/googleapiclient/discovery.py
@@ -219,7 +219,7 @@
 
     try:
       content = _retrieve_discovery_doc(
-        requested_url, discovery_http, cache_discovery, cache)
+        requested_url, discovery_http, cache_discovery, cache, developerKey)
       return build_from_document(content, base=discovery_url, http=http,
           developerKey=developerKey, model=model, requestBuilder=requestBuilder,
           credentials=credentials)
@@ -233,7 +233,8 @@
         "name: %s  version: %s" % (serviceName, version))
 
 
-def _retrieve_discovery_doc(url, http, cache_discovery, cache=None):
+def _retrieve_discovery_doc(url, http, cache_discovery, cache=None,
+                            developerKey=None):
   """Retrieves the discovery_doc from cache or the internet.
 
   Args:
@@ -264,6 +265,8 @@
   # document to avoid exceeding the quota on discovery requests.
   if 'REMOTE_ADDR' in os.environ:
     actual_url = _add_query_parameter(url, 'userIp', os.environ['REMOTE_ADDR'])
+  if developerKey:
+    actual_url = _add_query_parameter(url, 'key', developerKey)
   logger.info('URL being requested: GET %s', actual_url)
 
   resp, content = http.request(actual_url)
@@ -360,7 +363,9 @@
       # The credentials need to be scoped.
       credentials = _auth.with_scopes(credentials, scopes)
 
-      # Create an authorized http instance
+    # If credentials are provided, create an authorized http instance;
+    # otherwise, skip authentication.
+    if credentials:
       http = _auth.authorized_http(credentials)
 
     # If the service doesn't require scopes then there is no need for
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 941cfb0..a6635a3 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -449,7 +449,7 @@
 
     plus = build_from_document(
       discovery, base="https://www.googleapis.com/",
-      credentials=self.MOCK_CREDENTIALS)
+      credentials=None)
     # plus service requires Authorization
     self.assertIsInstance(plus._http, httplib2.Http)
     self.assertIsInstance(plus._http.timeout, int)
@@ -487,7 +487,7 @@
       http = HttpMockSequence([
         ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
         ])
-      zoo = build('zoo', 'v1', http=http, developerKey='foo',
+      zoo = build('zoo', 'v1', http=http, developerKey=None,
                   discoveryServiceUrl='http://example.com')
       self.fail('Should have raised an exception.')
     except HttpError as e:
@@ -506,6 +506,19 @@
     except HttpError as e:
       self.assertEqual(e.uri, 'http://example.com')
 
+  def test_key_is_added_to_discovery_uri(self):
+    # build() will raise an HttpError on a 400, use this to pick the request uri
+    # out of the raised exception.
+    try:
+      http = HttpMockSequence([
+        ({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
+        ])
+      zoo = build('zoo', 'v1', http=http, developerKey='foo',
+                  discoveryServiceUrl='http://example.com')
+      self.fail('Should have raised an exception.')
+    except HttpError as e:
+      self.assertEqual(e.uri, 'http://example.com?key=foo')
+
   def test_discovery_loading_from_v2_discovery_uri(self):
       http = HttpMockSequence([
         ({'status': '404'}, 'Not found'),