flutter Firestore许可被拒绝

zbq4xfa0  于 2022-12-30  发布在  Flutter
关注(0)|答案(4)|浏览(146)

我在编写flutter应用程序时在macOs上遇到了这个问题,相同的基础代码在我的Windows PC中工作,但在macOs中不工作。正如您在我的日志中看到的,应用程序已连接,但我仍然无法在我的Firestore数据库上发出请求
我改变了规则

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

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

但我也犯了同样的错误。

D/FirebaseAuth(11790): Notifying id token listeners about user ( tNGS8j375AYehEPDhZADPP80zLY2 ).
W/DynamiteModule(11790): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(11790): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(11790): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
W/Firestore(11790): (24.0.0) [Firestore]: Listen for Query(target=Query(usersProDEV where uuid == tNGS8j375AYehEPDhZADPP80zLY2 order by __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
E/flutter (11790): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

相同的代码在我的Windows PC工作

vmdwslir

vmdwslir1#

根据此post,您的应用访问互联网时出错。请检查您的网络连接:
检查您在androidManifest文件中是否具有适当的互联网权限(由Abhishek Dutt建议)。
如果您使用的是模拟器,请检查模拟器是否可以使用此帖子访问Internet。

第一版:

您能否尝试使用本文档中的示例代码进行身份验证:

service cloud.firestore {
      match /databases/{database}/documents {
        match /messages/{document=**} {
          allow read, update, delete: if request.auth.uid == resource.data.uid;
          allow create: if request.auth.uid != null;
        }
      }
    }

此代码只能用于阅读而不能用于写入:

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

z0qdvdin2#

几次之后,我意识到这个问题不仅是在MacOs下,而且是在模拟器上。通过进一步挖掘,我意识到我在项目上激活的AppCheck不接受模拟器下调试模式的所有请求。

dldeef67

dldeef673#

projectFolder/android/app/src/main/AndroidManifest.xml中添加以下内容:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7nbnzgx9

7nbnzgx94#

如文档https://firebase.google.com/docs/app-check/flutter/debug-provider中所述

await FirebaseAppCheck.instance.activate(
    // Set androidProvider to `AndroidProvider.debug`
    androidProvider: AndroidProvider.debug,   );

当Firebase尝试向后端发送请求时,您的应用会将本地调试令牌打印到调试输出。例如:

D DebugAppCheckProvider: Enter this debug secret into the allow list in
the Firebase Console for your project: 123a4567-b89c-12d3-e456-789012345678

enter image description here
在Firebase控制台的“应用检查”部分,从应用的溢出菜单中选择“管理调试令牌”。然后,注册您在上一步中登录的调试令牌。
Firebase console
注册令牌后,Firebase后端服务将接受它为有效。

相关问题