简体   繁体   中英

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

Step 2) New android app on firebase console, name: com.myapp.test (just as in the manifest)

Step 3) Download google-services.json and place it on the 'app' folder of my project

Step 4) Add the following lines:

Build.gradle - Project Level

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

Build.gradle - App level (at the bottom of the file)

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:

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)

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

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

Step 6) Run it and get the following:

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. 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. 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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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