google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

ValueError when API responds with 200 OK and content-length of 0

Open danielx opened this issue 6 years ago • 4 comments

It seems that the Admin Directory API now responds with 200 OK and content-length's of 0 as their "we have no content for you" responses instead of an empty JSON object as this client library assumes.

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
Traceback (most recent call last):
  File "/home/daniel/projects/test/vendor/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
    from . import file_cache
  File "/home/daniel/projects/test/vendor/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
    'file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
INFO:googleapiclient.discovery:URL being requested: GET https://www.googleapis.com/discovery/v1/apis/admin/directory_v1/rest
INFO:googleapiclient.discovery:URL being requested: GET https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&fields=users&alt=json&query=email%3Anoresults%2A
INFO:oauth2client.transport:Attempting refresh to obtain initial access_token
DEBUG:oauth2client.crypt:['REDACTED']
INFO:oauth2client.client:Refreshing access_token
Traceback (most recent call last):
  File "example.py", line 21, in <module>
    customer='my_customer', fields='users', query='email:noresults*').execute()
  File "/home/daniel/projects/test/vendor/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/daniel/projects/test/vendor/googleapiclient/http.py", line 857, in execute
    return self.postproc(resp, content)
  File "/home/daniel/projects/test/vendor/googleapiclient/model.py", line 216, in response
    return self.deserialize(content)
  File "/home/daniel/projects/test/vendor/googleapiclient/model.py", line 274, in deserialize
    body = json.loads(content)
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Environment details

  • OS: 18.04.1-Ubuntu
  • Python version: 2.7.17
  • pip version: 19.3.1
  • google-api-python-client version: 1.7.11

Steps to reproduce

  1. Do a .list operation with a query that returns zero results and only include the fields containing the results.
  2. You get a ValueError: No JSON object could be decoded since the API has started to return an 200 OK response with content-length of 0 when it previously always returned an {} in the body.

Snippet to reproduce

import logging

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

logging.basicConfig(level=logging.DEBUG)

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'key.json',
    scopes=[
        'https://www.googleapis.com/auth/admin.directory.user.readonly',
    ],
)
credentials = credentials.create_delegated('[email protected]')

admin_directory = build('admin', 'directory_v1', credentials=credentials)

admin_directory.users().list(
    customer='my_customer', fields='users', query='email:noresults*').execute()

danielx avatar Jan 17 '20 14:01 danielx

It seems that the Admin Directory API has been fixed and once again responds with at least {} for empty 2xx responses.

danielx avatar Jan 20 '20 08:01 danielx

We are facing this trouble too. Discovered recently that if you don't specify fields, it works properly.

8la avatar Jan 21 '20 14:01 8la

This was a Google server side issue and has since been resolved. You should not see any more 200 responses with a zero byte body.

jay0lee avatar Jan 24 '20 19:01 jay0lee

Closing as it seems to have been resolved.

busunkim96 avatar Jan 24 '20 21:01 busunkim96