异常:401 请求具有无效的身份验证凭据.预期的 OAuth 2 访问令牌、登录 Cookie 或其他有效的身份验证凭据



多年来,我见过一些人面临类似的问题,但我没有找到太多关于我的情况。

我有一个使用 python3 构建的后端。

  • 我正在使用firebase_admin作为库来连接到Firebase Cloud Firestore。
  • 然后,我将我的代码提交到 Github 并使用 Github 操作
  • 我正在将 docker 容器部署到 Google Cloud Run。
  • 这一切都在一段时间内工作正常
  • 一段时间后抛出以下异常:Exception: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

我做错了什么?

  • 我应该在每次调用函数时都连接吗?(在每次函数调用中移动数据库初始化(
  • 感觉令牌已过期。我可以以某种方式刷新它吗?

蟒蛇代码:

from firebase_admin import firestore, initialize_app
initialize_app()
db = firestore.client()
def get_info_from_firestore(name: str:
try:
data = db.collection(u'data').where(u'title', u'==', name).stream()
for rating in ratings:
return rating.to_dict()
return None
except Exception as e:
logging.warning(Exception: {e}')
return None

这个文件是从我使用 Flask 的根 python 文件导入的。

编辑:最后一件事可能会有所帮助,如果我重新部署我的容器而不进行任何更改,它会再次工作。

这可能是由于时间漂移而发生的:docker 中的时间与实时时间明显不同。重新启动后时间是同步的,但一段时间后它会漂移。谷歌不喜欢它。在此处查看有关此 WSL/容器问题的详细信息 https://github.com/microsoft/WSL/issues/4245 和此处 https://github.com/docker/for-win/issues/4526

最新更新