服务帐户的GCP Firestore Python API PermissionDenied错误

时间:2020-03-18 14:20:30

标签: firebase google-cloud-platform google-cloud-firestore google-cloud-datastore

我的目标是利用GCP的Firestore Python 3 API。我已经创建了一个服务帐户,并按照official documentation进行了设置。但是,对Firestore的任何请求仍然会导致google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions错误。我确保Firestore数据库规则允许在Firebase控制台中进行所有读取和写入操作。我已经将OwnerCloud Datastore Owner角色都添加到了我的服务帐户中,但是没有任何作用。我尝试过两种方式提供凭据,但两种方法都没有成功:

from google.cloud import firestore

# Method #1
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.getcwd() + "/credentials.json"
db = firestore.Client()

# Method #2
from google.oauth2.service_account import Credentials
db = firestore.Client(project="my-project-id",
                      credentials=Credentials.from_service_account_file("credentials.json"))

我用尽了所有选项,希望能解决此问题。

1 个答案:

答案 0 :(得分:1)

结果证明,执行此代码很重要。我是在GCP的Compute Engine VM实例中执行它的,显然必须在VM的Cloud API访问范围中启用相应的云服务。 This的答案特别解决了我的问题。