PERMISSION_DENIED:缺少权限或权限不足

时间:2020-06-06 18:28:06

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

我正在运行以下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;
    }
  }
}

1 个答案:

答案 0 :(得分: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}');