将Python客户端用于Google服务帐户file_cache时,“ImportError:file_cache不可用”

问题描述 投票:28回答:3

我正在使用G Suite的服务帐户进行完整的域名授权。我有一个只读访问Google日历的脚本。该脚本工作正常,但在“构建”服务时抛出错误(在后台线程上?)。这是代码:

from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
import urllib
import requests
from apiclient.discovery import build

cal_id = "[email protected]"

scopes                = ['https://www.googleapis.com/auth/calendar.readonly']
credentials           = ServiceAccountCredentials.from_json_keyfile_name('my_cal_key.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('[email protected]')
http_auth             = delegated_credentials.authorize(Http())

# This is the line that throws the error
cal_service  = build('calendar','v3',http=http_auth)

#Then everything continues to work normally
request = cal_service.events().list(calendarId=cal_id)
response = request.execute()

# etc...

抛出的错误是:

WARNING:googleapiclient.discovery_cache:file_cache is unavailable when using oauth2client >= 4.0.0
Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
    from google.appengine.api import memcache
ImportError: No module named 'google'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
    from oauth2client.contrib.locked_file import LockedFile
ImportError: No module named 'oauth2client.contrib.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
    from oauth2client.locked_file import LockedFile
ImportError: No module named 'oauth2client.locked_file'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
    from . import file_cache
  File "/Users/myuseraccount/anaconda3/lib/python3.5/site-packages/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
    'file_cache is unavailable when using oauth2client >= 4.0.0')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0

这里发生了什么,这是我能解决的问题吗?我已经尝试重新安装和/或升级google包。

python google-calendar-api
3个回答
59
投票

在这里聚会迟到但我今天遇到了类似的问题,并找到答案here

只解决错误:file_cache is unavailable when using oauth2client >= 4.0.0

解:

改变你的discovery.build()有字段cache_discovery=False

discover.build(api, version, http=http, cache_discovery=False)

编辑:

正如@Chronial所说,这将禁用缓存。

可以找到一个不禁用缓存的解决方案here


10
投票

模块“google-api-python-client”的代码头说...

install_requires = [
     'httplib2>=0.9.2,<1dev',
     'oauth2client>=1.5.0,<5.0.0dev',    <<=============
     'six>=1.6.1,<2dev',
     'uritemplate>=3.0.0,<4dev',
]

所以,我已经卸载了oauth2client版本4.0.0

然后,我从offial python网站https://pypi.python.org/pypi/oauth2client/1.5.2的tar.gz文件中下载了oauth2client 1.5.2

我已安装此下载文件,因此我有1.5.2版本的oauth2client

Package                  Version
------------------------ ---------
certifi                  2016.9.26
discovery                0.0.4
distribute               0.7.3
future                   0.16.0
google-api-python-client 1.5.5
httplib2                 0.9.2
oauth2client             1.5.2
pefile                   2016.3.28
pip                      9.0.1
pyasn1                   0.1.9
pyasn1-modules           0.0.8
PyInstaller              3.2
pypiwin32                219
requests                 2.11.1
rsa                      3.4.2
setuptools               28.8.0
six                      1.10.0
uritemplate              3.0.0

之后,ALL再次正常工作,没有警告信息。


0
投票

要使用Google API for Python客户端,您需要先安装它,因为Google API不是内置在Python模块中。该指令可在Install the Library中找到。

安装

您可以使用包管理器,也可以手动下载并安装Python客户端库:

托管安装

使用pip或setuptools来管理你的安装(你可能需要先运行sudo):

点子(首选):

$ pip install --upgrade google-api-python-client

Setuptools:使用setuptools包中包含的easy_install工具:

$ easy_install --upgrade google-api-python-client

© www.soinside.com 2019 - 2024. All rights reserved.