我得到了错误

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());
             }
         }
     });

当前回答

时间限制可能已过

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);

其他回答

这里的变化 将false设为true

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

并发布新的规则

如果有人登陆这里试图使用服务帐户访问Firestore:

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

另一个原因是AppCheck。我在一个新项目上启用了它(创建~ 2022年5月),但还没有完成集成步骤,导致“缺少或权限不足”错误。

要解决此问题,首先完成Firebase中AppCheck部分中列出的AppCheck设置步骤。我在我的web应用程序中使用了ReCAPTCHA提供者,你需要复制ReCAPTCHA公钥来在你的代码库中使用。

接下来,在你初始化firebase应用的任何地方添加AppCheck初始化代码。我的在React中是这样的:

  import { initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check';

  // ......

  // Initialize Firebase
  const app = initializeApp(firebaseConfig);
  const analytics = getAnalytics(app);

  self['FIREBASE_APPCHECK_DEBUG_TOKEN'] = true;
  // Pass your reCAPTCHA v3 site key (public key) to activate(). Make sure this
  // key is the counterpart to the secret key you set in the Firebase console.
  initializeAppCheck(app, {
    provider: new ReCaptchaV3Provider('PASTE KEY HERE'),

    // Optional argument. If true, the SDK automatically refreshes App Check
    // tokens as needed.
    isTokenAutoRefreshEnabled: true,
  });

注意FIREBASE_APPCHECK_DEBUG_TOKEN行在浏览器控制台中打印了一个调试令牌,你需要将它复制回AppCheck下的Firebase控制台中以完成设置,之后你可以注释/删除该行。

这解决了我的问题。

进一步的信息:

https://firebase.google.com/docs/app-check/web/recaptcha-provider https://firebase.google.com/docs/app-check/web/debug-provider?authuser=0&hl=en

转到firebase中的规则,编辑规则.....(提供时间戳或设置为false) 我的解决方案。

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

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

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;
}

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