【问题标题】:Firebase cloud firestore not working in flutterFirebase Cloud Firestore 无法正常工作
【发布时间】:2020-11-01 09:18:01
【问题描述】:

我正在构建这个使用 Cloud Firestore 的应用程序。问题是firestore不工作。这是我正在使用的代码

Db.dart

class Db{

  // collection reference
  static final CollectionReference _collectionReference = Firestore.instance.collection("codes");

  static Future addOrUpdateCode(String code , double lat , double long) async {
    return await _collectionReference.document(code).setData({
      'lat':lat,
      'long':long
    });
  }

}

我在 pubspec.yaml 中添加了这一行

cloud_firestore: ^0.13.7

我收到这些错误

I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8
W/Firestore(13145): (21.3.0) [FirestoreCallCredentials]: Failed to get token: com.google.firebase.FirebaseException: An internal error has occurred. [ yRequests to this API securetoken.googleapis.com method google.identity.securetoken.v1.SecureToken.GrantToken are blocked.�
W/Firestore(13145): #type.googleapis.com/google.rpc.Helpq
W/Firestore(13145): o
W/Firestore(13145):  Google developer console API keyKhttps://console.developers.google.com/project/280924879656/apiui/credential ].
I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8
W/Firestore(13145): (21.3.0) [FirestoreCallCredentials]: Failed to get token: com.google.firebase.FirebaseException: An internal error has occurred. [ yRequests to this API securetoken.googleapis.com method google.identity.securetoken.v1.SecureToken.GrantToken are blocked.�
W/Firestore(13145): #type.googleapis.com/google.rpc.Helpq
W/Firestore(13145): o
W/Firestore(13145):  Google developer console API keyKhttps://console.developers.google.com/project/280924879656/apiui/credential ].
I/BiChannelGoogleApi(13145): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@b61eba8

在云控制台中启用了以下 API

通过电子邮件/密码注册也在 firebase 中启用

我正在使用这些规则

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

我相信这是足够的信息。感谢您的宝贵时间。

【问题讨论】:

    标签: firebase flutter dart google-cloud-platform google-cloud-firestore


    【解决方案1】:

    Firestore 安全规则配置不正确。见Get started with Cloud Firestore Security Rules

    既然你提到你正在实施 Firebase 身份验证,你会想要他们提供的示例:

    // Allow read/write access on all documents to any user signed in to the application
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth != null;
        }
      }
    }
    

    但请注意,您可能还希望通过 自定义令牌 进行多级管理。请参阅Create Custom Tokens,您可能最终会得到类似允许管理员访问所有内容的规则,然后您将从那里缩小访问范围,例如:

    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.auth.token.admin == true;
        }
        match /users/{userId} {
          allow read, write: if request.auth.uid == userId;
        }
      }
    }
    

    【讨论】:

    • 它有效。我还必须在云控制台中启用 Token API。
    【解决方案2】:

    这些规则:

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

    在任何情况下都拒绝对所有用户进行读/写访问。您必须将它们更改为以下内容:

    // Allow read/write access to all users under any conditions
    // Warning: **NEVER** use this rule set in production; it allows
    // anyone to overwrite your entire database.
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if true;
        }
      }
    }
    

    或使用timestamp.date:

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read, write: if request.time < timestamp.date(2020, 07, 28);
        }
      }
    }
    

    这将允许读写直到指定的时间。

    https://firebase.google.com/docs/firestore/security/get-started#allow-all

    【讨论】:

      猜你喜欢
      • 2021-07-19
      • 2021-12-12
      • 2021-09-27
      • 2019-03-13
      • 2020-07-24
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多