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

时间:2020-05-24 09:48:59

标签: python-3.x firebase google-cloud-platform google-cloud-firestore github-actions

这些年来,我已经看到一些人面临类似的问题,但是我的案子并没有太多发现。

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

  • 我正在使用firebase_admin作为连接到Firebase Cloud Firestore的库。
  • 然后我将代码提交给Github并使用Gi​​thub Actions
  • 我正在将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.

我在做什么错?

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

Python代码:

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根目录导入的。

编辑:如果我在不做任何更改的情况下重新部署容器,那么最后一件可能会有所帮助。

1 个答案:

答案 0 :(得分:0)

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

查看有关此 WSL/容器问题的更多信息