firestore:PERMISSION_DENIED:缺少权限或权限不足

时间:2017-10-05 16:03:42

标签: android firebase google-cloud-firestore

我收到了错误

  

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

22 个答案:

答案 0 :(得分:56)

  

注意:这完全关闭了数据库的安全性,使其无需身份验证即可写入!这不是推荐的解决方案。

它对我有用。

进入数据库 - &gt;  规则 - &gt;

allow read, write: if false; 更改为 true;

答案 1 :(得分:10)

转到数据库-> 规则

然后更改为以下规则

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

答案 2 :(得分:6)

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

我通过在GCP的IAM设置中为服务帐户授予Service Account User角色以及Cloud Datastore User角色来解决了这个问题。

答案 3 :(得分:3)

确保您的数据库不为空,您的查询不适用于不存在的收集

答案 4 :(得分:3)

指定安全规则后,我还出现“缺少权限或权限不足”错误。事实证明,这些规则默认为非递归!也就是说,如果您编写了类似的规则

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

relevant section of the documentation上了解更多信息。

答案 5 :(得分:2)

因此,在我的情况下,我具有以下数据库规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /stories/{story} {
      function isSignedIn() {
        return request.auth.uid != null;
      }

      allow read, write: if isSignedIn() && request.auth.uid == resource.data.uid
    }
  }
}

您会看到uid文档上有一个story字段,用于标记所有者。

然后在我的代码中,我查询了所有故事(Flutter):

Firestore.instance
          .collection('stories')
          .snapshots()

它失败了,因为我已经通过不同的用户添加了一些故事。 要解决此问题,您需要向查询中添加条件:

Firestore.instance
          .collection('stories')
          .where('uid', isEqualTo: user.uid)
          .snapshots()

此处有更多详细信息:https://firebase.google.com/docs/firestore/security/rules-query

答案 6 :(得分:2)

当前时间是2020年6月,默认情况是按时间定义火力地堡。 确定满足您需求的时间。

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

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

答案 7 :(得分:2)

检查服务帐户是否以适当的角色(例如编辑者)添加到IAM & Admin https://console.cloud.google.com/iam-admin/iam

答案 8 :(得分:1)

问题是您在对用户进行身份验证之前尝试将数据读取或写入实时数据库或Firestore。请尝试检查您的代码范围。 希望能有所帮助!

答案 9 :(得分:1)

时间限制可能已结束

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

答案 10 :(得分:1)

我在Firebase Admin中遇到此错误,解决方案是正确配置Firebase Admin following this link

答案 11 :(得分:1)

如果您尝试使用Java Swing应用程序。

  1. 转到Firebase控制台>项目概述>项目设置

  2. 然后转到“服务帐户”选项卡,然后单击“生成新私钥”。

  3. 您将获得一个.json文件,并将其放置在已知路径中
  4. 然后转到“我的电脑属性”,“高级系统设置”,“环境变量”。
  5. 使用您的json文件路径创建新的路径变量“ GOOGLE_APPLICATION_CREDENTIALS”值。

答案 12 :(得分:1)

上面投票的答案对数据库的健康很危险。您仍然可以使数据库仅可用于读取而不能用于写入:

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

答案 13 :(得分:0)

npm i-将firebase保存为@ angular / fire

在应用程序模块中,请确保您已导入

import { AngularFireModule } from '@angular/fire';
import { AngularFirestoreModule } from '@angular/fire/firestore';

进口

AngularFireModule.initializeApp(environment.firebase),
    AngularFirestoreModule,
    AngularFireAuthModule,

实时数据库规则确保您拥有

{
  /* Visit  rules. */
  "rules": {
    ".read": true,
    ".write": true
  }
}

在云存储规则中,请确保您拥有

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

答案 14 :(得分:0)

对我来说,这是日期问题。更新了它,问题解决了。

允许读/写:

 if request.time < timestamp.date(2020, 5, 21);

编辑:如果您仍然感到困惑并且无法找出问题所在,请查看Firebase控制台中的“规则”部分。

答案 15 :(得分:0)

此外,如果代码中的集合引用与firebase上的集合名称不匹配,则可能会出现此错误。

例如,firebase上的集合名称为users,但您使用db.collection("Users")db.collection("user")引用它

它也区分大小写。

希望这对某人有帮助

答案 16 :(得分:0)

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

Model.find(query)
  .lean()
  .populate('user', 'firstName lastName')
  .then(rec => {
    // do something
  })
  .catch(err => {
    // handle error
  })

答案 17 :(得分:0)

转到firebase控制台=> cloud firestore数据库并添加允许用户读写的规则。

=>允许读写

答案 18 :(得分:0)

进入 Firestore 的数据库 > 规则:

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

更多信息:https://firebase.google.com/docs/rules/rules-language?authuser=0

截至目前 100% 工作!?

答案 19 :(得分:-1)

https://console.firebase.google.com

开发->数据库->规则->设置读取,写入->真

enter image description here

enter image description here

答案 20 :(得分:-1)

在这种情况下,您的规则应该是这样,但是消息提示这不是建议的方式,但是如果您这样做,则可以进行插入而不会出现权限错误

reg_pipeline = [initializer,
                preprocesser]


for func in reg_pipeline:
    func(init_params)

答案 21 :(得分:-1)

  

警告:这样,每个人(甚至是未经身份验证的用户)都可以对所有数据库进行读写访问。可能不是你想要的!

进入数据库 - &gt;规则 - &gt; 并且这样做

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