使用 Google 服务帐户 file_cache 的 Python 客户端时出现“ImportError: file_cache is unavailable”

2024-01-01

我正在使用具有完全域委托的 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] /cdn-cgi/l/email-protection"

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] /cdn-cgi/l/email-protection')
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包裹。


我参加这里的聚会有点晚了,但今天我遇到了类似的问题并找到了答案here https://github.com/google/google-api-python-client/issues/299

仅错误的解决方案:file_cache is unavailable when using oauth2client >= 4.0.0

解决方案:

改变你的discovery.build()拥有领域cache_discovery=False i.e

discovery.build(api, version, http=http, cache_discovery=False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Google 服务帐户 file_cache 的 Python 客户端时出现“ImportError: file_cache is unavailable” 的相关文章

  • 从 asyncio 子进程获取实时输出

    我正在尝试使用 Python asyncio 子进程来启动交互式 SSH 会话并自动输入密码 实际用例并不重要 但它有助于说明我的问题 这是我的代码 proc await asyncio create subprocess exec ssh
  • 从 Python 中的 message_from_string() 获取发件人地址

    有人可以告诉我如何在Python中从email message from string 获取发件人地址吗 谢谢 我试过 message email message from string email text from message Fr
  • setColumnStretch 和 setRowStretch 如何工作

    我有一个使用构建的应用程序PySide2它使用setColumnStretch用于柱拉伸和setRowStretch用于行拉伸 它工作得很好 但我无法理解它是如何工作的 我参考了 qt 文档 但它对我没有帮助 我被困在括号内的两个值上 例如
  • Django 营业时间

    我想添加诊所的营业时间 我已经对此进行了调查在 Django 中实现 开放时间 的任何现有解决方案 https stackoverflow com questions 8128143 any existing solution to imp
  • 如何使用 Pycharm 运行 fast-api 服务器?

    我有一个简单的 API 函数 如下所示 from fastapi import FastAPI app FastAPI app get async def read root return Hello World 我正在使用启动服务器uvi
  • 将 numpy 数组及其大小写入二进制文件

    我需要将 2D numpy 数组写入文件 包括其尺寸 以便我可以从 C 程序中读取它并创建相应的数组 我编写了一些简单的代码来保存数组 并且可以从 C 读取它 但是如果我尝试先写入数组的大小 它总是会给我一个错误 这是我的简单 python
  • 如何同时有效地运行多个 Pytorch 进程/模型? Traceback:分页文件太小,无法完成此操作

    背景 我有一个非常小的网络 我想用不同的随机种子进行测试 该网络几乎只使用了我的 GPU 计算能力的 1 因此理论上我可以同时运行 50 个进程来同时尝试许多不同的种子 Problem 不幸的是我什至无法在多个进程中导入 pytorch 当
  • 如何用pygame画一条虚线?

    我需要在坐标系上绘制正弦波和余弦波 就像在this https i stack imgur com DGI8g png图片 除了没能代表以外 我所有的工作都做得很好虚线和曲线与 pygame 一致 我有与我需要的类似的东西 但我怎样才能让它
  • Python SQLite3 SQL注入漏洞代码

    我知道下面的代码片段由于 format 的原因很容易受到 SQL 注入的攻击 但我不知道为什么 有谁明白为什么这段代码容易受到攻击以及我从哪里开始修复它 我知道这些代码片段使输入字段保持打开状态 以便通过 SQL 注入执行其他恶意命令 但不
  • 熊猫系列到二维数组

    所以 我使用了来自的答案将二维数组放入 Pandas 系列中 https stackoverflow com questions 38840319 put a 2d array into a pandas series将 2D numpy
  • 如何使用 Python 在表单中选择选项?

    我想知道如何以格式如下的形式选择选项 td align left td
  • 如何在 tkinter 后台运行函数[重复]

    这个问题在这里已经有答案了 我是 GUI 编程新手 我想用 tkinter 编写一个 Python 程序 我想要它做的就是在后台运行一个可以通过 GUI 影响的简单函数 该函数从 0 计数到无穷大 直到按下按钮为止 至少这是我想要它做的 但
  • 使用 Popen 打开进程并获取 PID

    我正在开发一个漂亮的小功能 def startProcess name path Starts a process in the background and writes a PID file returns integer pid Ch
  • 在添加数据之前使用 Python gdata 清除工作表中的行

    我有一个 Google 电子表格 我使用 python 脚本和 gdata 库填充值 如果我多次运行脚本 它会将新行附加到工作表中 我希望脚本在填充之前首先清除行中的所有数据 这样每次运行时我都会有一组新的数据脚本 我尝试过使用 Updat
  • 根据标签位置计算 Pandas DataFrame 的索引

    我正在尝试计算标签的索引Pandas https pandas pydata org DataFrame在每一列中 基本上我有以下内容DataFrame d col1 label1 label2 label3 col2 label2 lab
  • model.predict() 返回类而不是概率

    Hello 我是第一次使用 Keras 我训练并保存了一个模型 作为 json 文件及其权重 该模型旨在将图像分为 3 个类别 我的编译方法 model compile loss categorical crossentropy optim
  • 如何在Python中不使用库函数将字符串转换为整数?

    我正在尝试转换 a 546 to a 546 不使用任何库函数 我能想到的 最纯粹 gt gt gt a 546 gt gt gt result 0 gt gt gt for digit in a result 10 for d in 01
  • 如何从 Anaconda 更新 Pandas 以及最后是否可以使用 eclipse

    我已经使用以下文档通过 Anaconda 安装了 Python http www kevinsheppard com images 0 09 Python introduction pdf http www kevinsheppard co
  • 无法将 librosa 与 python 3 一起使用

    我已经在 Windows 上的 ubuntu 子系统上使用 pip3 正确安装了 librosa 但是当我尝试执行像这样的简单程序时 import librosa data sr librosa load sound mp3 print d
  • 基于 Web 请求在 Airflow 上运行作业

    我想知道是否可以在通过 HTTP 收到请求时执行气流任务 我对 Airflow 的调度部分不感兴趣 我只是想用它来代替芹菜 因此 示例操作如下所示 用户提交一份表格 请求某些报告 后端接收请求并向用户发送请求已收到的通知 然后后端使用 Ai

随机推荐