Search code examples
pythongoogle-api-clientgoogle-api-python-clientgoogle-cloud-vision

WARNING:oauth2client.util:build() takes at most 2 positional arguments (3 given)


I am doing the "Label Detection" tutorial for the Google Cloud Vision API.
When I pass an image to the command like so I expect to get back some json telling me what is in the image.

However, I am getting this error instead.

 😈   >python label_request.py faulkner.jpg 
No handlers could be found for logger "oauth2client.util"
WARNING:root:No module named locked_file
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery_cache/__init__.py", line 38, in autodetect
    from . import file_cache
  File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery_cache/file_cache.py", line 32, in <module>
    from oauth2client.locked_file import LockedFile
ImportError: No module named locked_file
Traceback (most recent call last):
  File "label_request.py", line 44, in <module>
    main(args.image_file)
  File "label_request.py", line 18, in main
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)
  File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 202, in build
    raise e
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559).">

Lots going on here.
But the Project API is enabled.
So this is part of the error message is erroneous.

It seems that "there was a change in the newest version of the oauth2client, v2.0.0, which broke compatibility with the google-api-python-client module".
https://stackoverflow.com/a/35492604/2341218

I applied this fix ...

pip install --upgrade git+https://github.com/google/google-api-python-client

After applying this fix, I get fewer errors ...

 😈   >python label_request.py faulkner.jpg 
No handlers could be found for logger "oauth2client.util"
Traceback (most recent call last):
  File "label_request.py", line 44, in <module>
    main(args.image_file)
  File "label_request.py", line 18, in main
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)
  File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 209, in build
    raise e
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559).">

It appears that this error message: "No handlers could be found for logger "oauth2client.util" is actually masking a more detailed warning/error message and that I can see the more detailed one by adding this code ...

import logging 
logging.basicConfig()

https://stackoverflow.com/a/29966147/2341218

 😈   >python label_request.py faulkner.jpg 
WARNING:oauth2client.util:build() takes at most 2 positional arguments (3 given)
Traceback (most recent call last):
  File "label_request.py", line 47, in <module>
    main(args.image_file)
  File "label_request.py", line 21, in main
    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)
  File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 209, in build
    raise e
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://vision.googleapis.com/$discovery/rest?version=v1 returned "Project has not activated the vision.googleapis.com API. Please enable the API for project google.com:cloudsdktool (#32555940559).">

So no I am stuck on this error message:
WARNING:oauth2client.util:build() takes at most 2 positional arguments (3 given)

It has been suggested that this error can be avoided by using named parameters instead of positional notation.
https://stackoverflow.com/a/16643215/2341218

However, I am uncertain exactly where I might make this change.
I don't actually see the oauth2client.util:build() function in the code.
Here is the google code (slightly modified):

 😈   >cat label_request.py
import argparse
import base64
import httplib2

from apiclient.discovery import build
from oauth2client.client import GoogleCredentials

import logging
logging.basicConfig()

def main(photo_file):
  '''Run a label request on a single image'''

  API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1'
  http = httplib2.Http()

  credentials = GoogleCredentials.get_application_default().create_scoped(
      ['https://www.googleapis.com/auth/cloud-platform'])
  credentials.authorize(http)

  service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE)

  with open(photo_file, 'rb') as image:
    image_content = base64.b64encode(image.read())
    service_request = service.images().annotate(
      body={
        'requests': [{
          'image': {
            'content': image_content
           },
          'features': [{
            'type': 'LABEL_DETECTION',
            'maxResults': 1,
           }]
         }]
      })
    response = service_request.execute()
    label = response['responses'][0]['labelAnnotations'][0]['description']
    print('Found label: %s for %s' % (label, photo_file))
    return 0

if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  parser.add_argument(
    'image_file', help='The image you\'d like to label.')
  args = parser.parse_args()
  main(args.image_file)

Solution

  • I've had the exactly same problem and I just solved doing this code line (you have to have gcloud installed):

    gcloud auth activate-service-account --key-file <service-account file.json>
    

    and then:

    $ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
    

    Hope that helps!