我得到了错误

gettingdocuments.com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED:缺少或权限不足。

对于下面关于else语句的代码

db.collection("users")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
             if (task.isSuccessful()) {
                 for (DocumentSnapshot document : task.getResult()) {
                     s(document.getId() + " => " + document.getData());
                 }
             } else {
                 s("Error getting documents."+ task.getException());
             }
         }
     });

当前回答

有很多很好的答案,但由于这是Firestore许可拒绝错误的顶级谷歌响应,我想我应该为初学者和新手添加一个答案。

为什么要设置安全规则?

如果您编写自己的后端,您将让用户向服务器请求一些东西,服务器将决定允许他们做什么。例如,您的服务器不允许user1删除user2的所有数据。

但是由于你的用户直接与Firebase交互,你不能真正信任你的用户发送给你的任何东西。例如,User1可以将删除请求中的用户id更改为'user2'。

Firestore安全规则是你告诉Firestore即你的后端服务器,谁被允许读取和写入什么数据。

Firestore团队的这个视频非常有用。

https://www.youtube.com/watch?v=eW5MdE3ZcAw

如果你遇到“权限缺失或权限不足”的错误,我强烈建议你在尝试其他任何东西之前观看完整的22分钟视频。

我从1/10到7/10理解了安全规则是如何工作的,以及为什么我只从这个视频中得到了错误。

入门指南也很有用,可以帮助回答视频中没有涵盖的问题。https://firebase.google.com/docs/firestore/security/get-started

其他回答

时间限制可能已过

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone on the internet to view, edit, and delete
    // all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // your app will lose access to your Firestore database
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2020,7, 1);
    }
  }
}

这一行更改了日期:

 allow read, write: if request.time < timestamp.date(2020,7, 1);

对我来说,问题是AppCheck在我的Firestore控制台也被激活了。 所以我必须按照指南中所述的应用程序检查颤振指南

https://firebase.google.com/docs/app-check/flutter/debug-provider?hl=it&authuser=0

打开androidDebugProvider: true,从控制台复制调试令牌,并将其粘贴到Firestore部分(AppCheck—> app—> add调试令牌),它立即工作。

确保你的数据库不是空的,你的查询是不存在的集合

进入数据库-> 规则- - - >

发展:

更改允许读,写:如果为false;真正的;

注意:它只是用于开发目的的快速解决方案,因为它将关闭所有安全性。因此,不建议在生产中使用。

生产:

如果从firebase验证:更改允许读,写:如果为false;请求。Auth != null;

也许你该把日期去掉

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if
          request.time < timestamp.date(2021, 12, 12);
    }
  }
}