我得到了错误

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:

我通过在GCP的IAM设置中授予Service - Account Cloud Datastore User角色之外的Service Account User角色解决了这个问题。

其他回答

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

转到Apple Certificates, Identifiers & Profiles: 选择您的密钥上传firebase并进行检查: 访问DeviceCheck和apptest api以获取您关联的数据

在这里输入图像描述

此时,即2020年6月,默认情况下firebase是按时间定义的。 为满足自己的需要而安排好时间。

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

请注意:你的数据库仍然对任何人开放。我建议,请阅读文档并以对您有用的方式配置DB。

进入数据库->规则:

然后更改如下规则

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

以下

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

在指定安全规则后,我还出现了“缺少或权限不足”错误。事实证明,默认情况下规则不是递归的!例如,如果你写了一个规则

match /users/{userId} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

该规则将不适用于/users/{userId}下的任何子集合。这就是我犯错误的原因。

我通过指定规则来修复它:

match /users/{userId}/{document=**} {
  allow read, write: if request.auth != null && request.auth.uid == userId;
}

在文档的相关部分中阅读更多信息。