【问题标题】:PERMISSION_DENIED: Missing or insufficient permissionsPERMISSION_DENIED:权限缺失或不足
【发布时间】:2020-09-25 21:25:15
【问题描述】:

我正在运行以下 firebase 代码

    FirebaseApp app = await FirebaseApp.configure(
          name: 'weather app',
          options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
          ),
        );
        Firestore firestore = Firestore(app: app);
        FirebaseAuth.fromApp(app).signInAnonymously();

如果我第一次在模拟器中运行这段代码,它会抛出以下错误

 I/flutter (17766): getConfigState ERROR 2 PlatformException(Error performing get, PERMISSION_DENIED: Missing or insufficient permissions., null)
W/Firestore(17766): (21.3.0) [Firestore]: Listen for Query(weather-api-info/owm) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

但是,如果我只是使用热重载或完全重载​​来重新加载应用程序,那么它不会抱怨。同样,仅当应用程序在设备上首次运行时才会出现此问题(我可以通过卸载应用程序并重试来重现此问题)

Cloud Firestore 身份验证如下所示(我不打算设置任何登录或身份验证,但也不想保持打开状态

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

【问题讨论】:

  • 您的安全规则要求用户在查询时登录。
  • 那个@DougStevenson,有没有办法让我保持访问关闭但没有登录,或者如何以管理员身份登录..?另外,为什么相同的代码第二次运行没有抱怨?
  • 无法将 Firestore 查询仅限于您的应用。您必须使用 Firebase 身份验证来限制访问。
  • @KenWhite 刚刚完成,点击保存按钮,太快了……
  • @DougStevenson 谢谢,但是,如果我第二次使用 hotreload 重新加载代码,同样的代码可以工作..**输出** I/flutter (17766): getConfigState from firebase https://community-open-weather-map.p.rapidapi.com/weather/

标签: firebase flutter google-cloud-firestore firebase-security


【解决方案1】:

所以,我能够自己解决这个问题。简而言之,我没有为异步调用正确设置代码。 以下是我修改的(将下面提到的代码与原帖中的代码进行比较)

Future<void> getOWMFirestoreData() async {
  try {
    FirebaseApp app = await FirebaseApp.configure(
      name: 'weather app',
      options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
      ),
    );
    await FirebaseAuth.fromApp(app).signInAnonymously();
    Firestore firestore = Firestore(app: app);

    var docs =
        await firestore.collection('XXX-info').document('owm').get();
    debugPrint('getOWMFirestoreData Succesfull ${docs.exists}');

【讨论】:

    猜你喜欢
    • 2019-01-06
    • 2019-05-05
    • 1970-01-01
    • 2020-02-11
    • 2020-09-06
    • 2019-10-24
    • 2018-09-12
    • 2020-10-14
    • 2018-03-17
    相关资源
    最近更新 更多