简体   繁体   English

Firebase和Android Studio,无法过去:W / SyncTree:在/ message侦听失败:DatabaseError:权限被拒绝

[英]Firebase & Android Studio, unable to get past: W/SyncTree: Listen at /message failed: DatabaseError: Permission denied

Steps to replicate what I do: 复制我所做的步骤:

Step 1) New Project example name: com.myapp.test 步骤1)新建项目示例名称:com.myapp.test

Step 2) New android app on firebase console, name: com.myapp.test (just as in the manifest) 步骤2)在Firebase控制台上新建Android应用,名称:com.myapp.test(与清单中一样)

Step 3) Download google-services.json and place it on the 'app' folder of my project 第3步)下载google-services.json并将其放在我项目的“ app”文件夹中

Step 4) Add the following lines: 步骤4)添加以下行:

Build.gradle - Project Level Build.gradle- 项目级别

classpath 'com.google.gms:google-services:3.0.0'

Build.gradle - App level (at the bottom of the file) Build.gradle- 应用程序级别 (在文件底部)

apply plugin: 'com.google.gms.google-services'

I also take the opportunity to add a couple of dependencies from Firebase I'm going to use, such as: 我还借此机会从我将要使用的Firebase中添加了一些依赖项,例如:

compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.android.gms:play-services:9.6.1'

Step 5) Main Activity (on create) 步骤5)主要活动(创建时)

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");

    myRef.setValue("Hello, World!");

Step 6) Run it and get the following: 步骤6)运行它并获得以下信息:

W/SyncTree: Listen at /message failed: DatabaseError: Permission denied
W/MainActivity: Failed to read value.
com.google.firebase.database.DatabaseException: Firebase Database error: Permission denied
     at com.google.firebase.database.DatabaseError.toException(Unknown Source)
     at com.myapp.test.MainActivity$1.onCancelled(MainActivity.java:50)
     at com.google.android.gms.internal.zzajp.zza(Unknown Source)
     at com.google.android.gms.internal.zzakn.zzcxi(Unknown Source)
     at com.google.android.gms.internal.zzaks$1.run(Unknown Source)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5417)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I've tried starting from scratch at least 5 times, different names and all. 我尝试从头开始至少5次,使用不同的名称。 Even adding fingerprint. 甚至添加指纹。 However, no matter what I do, I can't shake this error. 但是,无论我做什么,我都无法摆脱这个错误。

I can make it work by going to the 'Permissions' tab on the console and change 'auth != null' to true. 通过转到控制台上的“权限”选项卡并将“ auth!= null”更改为true,可以使其工作。 But that's a bandaid, I need a permanent solution. 但这是一个创可贴,我需要一个永久的解决方案。

My hands are sort of tied here, ran out of things to try. 我的手被绑在这里,用尽了所有的尝试。 I do get the following messages that may or may not have something to do with the error above: 我确实收到以下消息,该消息可能与上面的错误有关,也可能与上面的错误无关:

I/InstantRun: Instant Run Runtime started. Android package is com.myapp.test, real application class is null.

And also: 并且:

W/DynamiteLoaderImpl: Failed to load module version: module com.google.android.gms.flags not found

The only permanent solution is to sign in the user of the app with Firebase Authentication. 唯一的永久解决方案是使用Firebase身份验证登录应用程序的用户。

The simplest way to do that is to sign in with anonymous authentication and only writing to the database when that succeeds: 最简单的方法是使用匿名身份验证登录,并仅在成功时才写入数据库:

mAuth.signInAnonymously()
     .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
       @Override
       public void onComplete(@NonNull Task<AuthResult> task) {
         if (task.isSuccessful()) {
           FirebaseDatabase database = FirebaseDatabase.getInstance();
           DatabaseReference myRef = database.getReference("message");
           myRef.setValue("Hello, World!");
         }
       }
     });

Either authenticate prior to writing to the DB or change your security rules to allow non authenticated users to read/write. 写入数据库之前先进行身份验证,或者更改安全规则以允许未经身份验证的用户读取/写入。

https://firebase.google.com/docs/database/security/ https://firebase.google.com/docs/database/security/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM