使用 Pyrebase 在 Firebase 数据库上的权限被拒绝

Permission denied on Firebase Database using Pyrebase

我已经成功地注册了一个用户,并在他们在相同的请求中注册后不久正确地注册了他们。同样在我的请求中,我尝试使用 python 包 Pyrebase 将用户信息存储在 Firebase 数据库中。

我已经正确初始化了我的 firebase 应用程序以及我的 flask 应用程序中的 firebase 数据库

config = {
    "apiKey": "apikey",
    "authDomain": "proj.firebaseapp.com",
    "databaseURL": "https://proj.firebaseio.com",
    "storageBucket": "proj.appspot.com",
    "messagingSenderId": "930158028418",
    "serviceAccount.json": "path/to/theServiceAccountStuff.json"
}

app = Flask(__name__)
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
firedb = firebase.database()

我正在创建这本词典

userInfo = {"email": email, "password": password, "stripeID": customer.id, "subscriptionID": subID,"subscription": "active", "startdate": "Today bro"}

并将其传递到我的 set 函数中,该函数应该(或者至少我认为是)将我的 JSON 对象保存到 Firebase 数据库中。

firedb.child("users").child(user['localId']).set(data=userInfo)

我正在尝试将用户存储在 "users" 下,然后使用自定义键作为他们的 localId 对他们进行分类。

出于某种原因,每次我点击上面的代码行时,都会出现这样的错误

requests.exceptions.HTTPError: [Errno 401 Client Error: Unauthorized for url: https://myproj.firebaseio.com/users/someid.json] {
  "error" : "Permission denied"
}

这些是我在 firebase 中的数据库规则

有什么我想念的吗?我什至更进一步删除了所有规则,以便任何人都可以访问它,而且我还放入了虚拟数据以查看它是否有效。虚拟数据是可能的,因为我手动输入所有信息。

您正在尝试使用 Pyrebase 写入 Firebase 实时数据库,但您的项目数据库配置为改用 Cloud Firestore。

这些数据库不一样。

如果您在托管服务上使用 Python,请考虑切换到 Firebase Admin SDK for Python. As you are using a service account, this would be the way to go. This will also allow you to make the choice between using Cloud Firestore and the Realtime Database

如果您在客户端设备上使用 Python,即使它是本地 Web 服务器,出于安全原因,您也不应使用服务帐户,而应选择客户端身份验证。如果您想使用 Pyrebase, at the time of writing, you will need to switch your project's database to use the Realtime Database using the dropdown next to the word "Database" in your screenshot. This is because Cloud Firestore does not currently have a REST API which Pyrebase uses as its backend. See the Firebase Realtime Database documentation 了解更多信息。

对我来说,这很有效:

Firebase Console -> Realtime Database -> Rules

编辑规则:

{
  "rules": {
    ".read": "true", 
    ".write": "true",
  }
}