Android 客户端上的 Firestore 集合组 PERMISSION_DENIED

use*_*701 2 android firebase firebase-security google-cloud-firestore

遵循文档的测试收集组 ( https://firebase.google.com/docs/firestore/security/rules-query#collection_group_queries_and_security_rules )

Firestore 安全规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   match /forums/{forumid}/posts/{post} {
      allow read: if true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在 Android 应用上查询(使用 Firestore 19.0.1)

FirebaseFirestore.getInstance().collectionGroup("posts").get().addOnSuccessListener { queryDocumentSnapshots ->
    Log.d(TAG, "queryDocumentSnapshots " + queryDocumentSnapshots.size())

}.addOnFailureListener {
    Log.d(TAG, "exception" + it)
}
Run Code Online (Sandbox Code Playgroud)

获取异常 PERMISSION_DENIED:权限缺失或不足。

Edw*_*Liu 10

您需要在路径中添加通配符变量以使其适用于集合组查询。如果您更仔细地阅读,那在文档中:)

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   match /{path=**}/posts/{post} {
      allow read: if true;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于像我这样寻找文档的人:https://firebase.google.com/docs/firestore/security/rules-query#secure_and_query_documents_based_on_collection_groups - 不要忘记文件顶部的规则版本(如 EdwinLiu 在他的示例中所示) (4认同)