WARNING:oauth2client.util:build() 最多接受 2 个位置参数(给定 3 个)

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

我正在为 Google Cloud Vision API 编写 "Label Detection" 教程。
当我像这样将图像传递给命令时,我希望返回一些 json 告诉我图像中的内容。

但是,我收到了这个错误。

    >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).">

这里发生了很多事情。
但是项目 API 启用的。
所以这部分错误信息是错误的。

好像是"there was a change in the newest version of the oauth2client, v2.0.0, which broke compatibility with the google-api-python-client module".

我应用了此修复程序...

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

应用此修复程序后,我得到的错误更少了...

    >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).">

这条错误信息似乎是: "No handlers could be found for logger "oauth2client.util" 实际上掩盖了更详细的 warning/error 消息 并且我可以通过添加此代码来查看更详细的内容...

import logging 
logging.basicConfig()

    >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).">

所以不,我卡在了这条错误消息上:
WARNING:oauth2client.util:build() 最多接受 2 个位置参数(给定 3 个)

有人建议可以通过使用命名参数而不是位置符号来避免此错误。

但是,我不确定我可以在何处进行此更改。
我实际上并没有在代码中看到 oauth2client.util:build() 函数。
这是 google 代码(稍作修改):

    >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)

我遇到了完全相同的问题,我刚刚解决了这个代码行(你必须安装 gcloud):

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

然后:

$ export GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>

希望对您有所帮助!

发现模块构建方法的文档位于此处: https://google-api-python-client.googlecode.com/hg/docs/epy/apiclient.discovery-module.html#build

调用语法为:

build(serviceName, version, http=None, discoveryServiceUrl=DISCOVERY_URI,
developerKey=None, model=None, requestBuilder=HttpRequest)

特别是,build 接受两个位置参数和最多五个可选的命名参数。如果你想传递一个名为 http 的 http 处理程序,你可以调用 build with

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

如果你这样打电话

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

构建将改用默认的 http 处理程序。无论哪种方式,您都不应该再看到参数警告。