首页 > 解决方案 > Firestore 错误:流以状态关闭:PERMISSION_DENIED

问题描述

我正在尝试对某些数据使用 firestore,但在 firestore 控制台错误中看不到任何数据:

(c4326c7) Stream closed with status: Status{code=PERMISSION_DENIED, description=Cloud Firestore API has not been used in project 508621789005 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firestore.googleapis.com/overview?project=508621789005 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., cause=null}.

虽然我允许读,写规则为真。

val db = FirebaseFirestore.getInstance()
    // Create a new user with a first and last name
            val user: MutableMap<String, Any> = HashMap()
            user["first"] = "Ada"
            user["last"] = "Lovelace"
            user["born"] = 1815

    // Add a new document with a generated ID
            db.collection("users")
                .add(user)
                .addOnSuccessListener { documentReference ->
                    Log.d(
                        TAG,
                        "DocumentSnapshot added with ID: " + documentReference.id
                    )
                }
                .addOnFailureListener { e -> Log.w(TAG, "Error adding document", e) }

规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
       allow read, write: if true;
    }
  }
}

标签: firebasekotlin

解决方案


用这个替换你的规则并尝试:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{multiSegment=**} {
      allow read, write;
    }
  }
}

推荐阅读