我有一个firestore数据库,我希望能够使用本地脚本将数据上传到其中:
#!/bin/env python3
from google.cloud import firestore # google-cloud-firestore==2.1.1
client = firestore.Client()
test = client.collection("test")
doc = next(test.stream())这会抛出google.api_core.exceptions.PermissionDenied: 403 Missing or insufficient permissions.
我通过gcloud和firebase命令登录,并且我的firestore规则是打开的:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}我是GCP项目的所有者,client.project是正确的项目。
我是不是漏掉了什么?
发布于 2021-05-30 06:24:57
看起来服务帐户可以工作,但您需要一个特殊的firebase帐户,而不是GCP帐户。
https://console.firebase.google.com/project/${projectId}/settings/serviceaccounts/adminsdk
在GCP中创建的服务帐户不起作用,即使它们具有管理员权限。
此外,与google.cloud.firestore相比,firebase_admin sdk似乎允许更多关于凭据的配置。
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate(f"/home/user/Downloads/{projectId}-firebase-adminsdk-{random...}.json")
firebase_admin.initialize_app(cred)
# or set GOOGLE_APPLICATION_CREDENTIALS to the account path
db = firestore.client()
test = db.collection("test")
doc = next(test.stream())https://stackoverflow.com/questions/67755058
复制相似问题