GCP Firestore Python凭据

2024-04-25 17:15:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在将数据从我的Linux虚拟机发送到GCP的Firestore时遇到问题。 我只是想更新数据库中的一个项目。我有一个关于证书的问题。根据我使用的方法,我会得到不同的错误,但是,我相信它们都来自同一个问题。在

请注意,我有一个服务帐户,具有json凭据。我知道这些证书仍然有效,因为我可以使用GCP的演讲来发短信。我唯一奇怪的行为是访问Firestore时。在

注: 我从GCP控制台而不是Firebase控制台使用Firestore。我发现它们的使用方式略有不同(主要是安全方面的东西)。在

如果我只是打开一个新的终端并运行python脚本(我不设置对我的凭证文件的任何引用)。数据已成功添加到数据库,但我收到以下警告:

UserWarning: Your application has authenticated using end user credentials from Google Cloud 
SDK. We recommend that most server applications use service accounts instead. If your 
application continues to use end user credentials from Cloud SDK, you might receive a "quota 
exceeded" or "API not enabled" error. For more information about service accounts, see 
https://cloud.google.com/docs/authentication/

另一方面,如果我在运行同一个python脚本之前运行export GOOGLE_APPLICATION_CREDENTIALS="folder/file.json"行。我将得到以下错误:

^{pr2}$

如果我在python文件中使用类似于db = firestore.Client(project="myproj-99999",credentials="folder/file.json")的行直接声明凭据文件,则会出现以下错误:

Traceback (most recent call last):
  File "sendToDB.py", line 23, in <module>
    update_create_if_missing(args.uid, args.words)
  File "sendToDB.py", line 9, in update_create_if_missing
    db = firestore.Client(project="earningstotext-251320",credentials="keys/gcpcmdlineuser.json")
  File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/client.py", line 105, in __init__
    project=project, credentials=credentials, _http=None
  File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/client.py", line 227, in __init__
    Client.__init__(self, credentials=credentials, _http=_http)
  File "/home/vagrant/.local/lib/python3.6/site-packages/google/cloud/client.py", line 130, in __init__
    raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
ValueError: This library only supports credentials from google-auth-library-python. 
See https://google-cloud-python.readthedocs.io/en/latest/core/auth.html 
for help on authentication with this library.

请注意,这里的readthedocs链接已断开。

本质上,我只希望能够使用python更新和读取Firestore,而不会出现任何错误或警告。 我也搞不懂为什么,只有一次(当我收到警告时)我没有设置任何凭证。在

这是一个与我的上一个问题here相似的问题。不过,我认为我的新信息需要另外一个问题。在


Tags: 文件inpyprojectjsoncloud警告init
1条回答
网友
1楼 · 发布于 2024-04-25 17:15:20

您需要将“云数据存储所有者”角色添加到您正在使用的服务帐户中。在

  1. 转到console.cloud.google.com的IAM&admin>;IAM部分
  2. 查找您正在使用的服务帐户,然后单击铅笔图标进行编辑。在
  3. 单击addanotherrole,然后搜索datastoreowner。在

参考号:https://cloud.google.com/firestore/docs/security/iam

相关问题 更多 >