简体   繁体   中英

Firebase Database Permission denied with read/write permissions set to true

I'm having trouble writing to my Firebase database in my simple Android app. I've set my database rules to be public:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Created a simple class Message :

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Message {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference dbRef = database.getReference();
    public String sender;
    public String message;

    public Message(){}

    public Message(String sender, String message) {
        this.sender = sender;
        this.message = message;
    }
}

And set a button to perform the onClick method writeNewMessage with these being the significant lines of code:

int counter = 0;

public void writeNewMessage(View view) {

        counter += 1;
        Message messageToWrite = new Message("John", "Simple message: " + counter);
        String messageId = String.valueOf(counter);

        dbRef.child("users").child(messageId).setValue(messageToWrite);
    }

When running, I receive the error:

W/RepoOperation: setValue at /users/1 failed: DatabaseError: Permission denied

I know this question seems simple and likely to have been repeated, but I have yet to find any that asks this problem without having their error be involved in authentication or some other component. The simplicity of the problem is what confuses me the most.

EDIT: I've also found this error as well:

Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.firebase.auth.ModuleDescriptor"

I've been changing firestore rules instead of DB... Hope it helps to someone dumb like me.

在此处输入图片说明

Complete Solution:

1). Firstly go to Authentication Tab and select SIGN-IN-METHOD turn Anonymous On

2). Secondly go to Database Tab and write Rule

{
"rules": {
".read": true,
".write": true
}}

3). Thirdly go to Storage Tab and write following rule

service firebase.storage {
 match /b/myapplicationname-f2266.appspot.com/o {
 match /{allPaths=**} {
 allow read, write;
}}}

Note This solution is only for testing purpose with following steps you are giving public permission for Read and Write your database.

This looks like problem with your google-services.json.

Make sure app name and client id are same as in firebase console.

If you are not sure re-download google-services.json from your project's console and add it your project.

I'm not entirely sure what the problem was, but I just deleted my project from Firebase and recreated it, deleting and installing the new google-services.json and it worked perfectly the second time.

Luckily this is a test app and my database was empty, but if you're in this situation and have valuable information stored in your account, I would recommend exporting it before trying this solution.

Maybe you should set your rules to: (I had the same problem. It works for me.)

// These rules require authentication
    {
      "rules": {
        ".read": "auth != null",
        ".write": "auth != null"
      }
    }

The reason for this error can also be in this thing described in the documentation https://firebase.google.com/docs/database/android/start

Note: To get a reference to a database other than a us-central1 default dstabase, you must pass the database URL to getInstance() (or Kotlin+KTX database()) . For a us-central1 default database, you can call getInstance() (or database) without arguments. You can find your Realtime Database URL in the Realtime Database section of the Firebase console. It will have the form https://.firebaseio.com (for us-central1 databases) or https://.firebasedatabase.app (for databases in all other locations).

Okay I have tried all above solution and failed. After a while I found that my Project has 2 modules and I call firebase in the second module instead of the main one, so the solution is to add the second one to the firebase project too, and it works.

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