Firebase Permission denied Error
Asked Answered
T

2

31

I am Very beginner to firebase and trying to get value from my database.

but it showing me same error every time.

    W/SyncTree: Listen at /child failed: FirebaseError: Permission denied

My firebase rules

    {
  "Condition" : "sunny",
  "child" : 5
}

my Androidmanifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mohit.firebase" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My mainactivity.java package com.mohit.firebase;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

public class MainActivity extends AppCompatActivity {

    TextView tv;
    Button bt1;
    Button bt2;
    Firebase mRootRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = (TextView) findViewById(R.id.button);
        bt1 = (Button) findViewById(R.id.button);
        bt2 = (Button) findViewById(R.id.button2);
        Firebase.setAndroidContext(this);
        mRootRef = new Firebase("https://superb-flag-126719.firebaseio.com/");
        Log.d("fb","Firebase Object: "  + String.valueOf(mRootRef));

    }

    @Override
    protected void onStart() {
        super.onStart();

        Firebase mchild = mRootRef.child("child");
        mchild.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String condition = (String) dataSnapshot.getValue();
                Log.d("fb","String get: " + condition);
                tv.setText(condition);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });
    }
}

my BuildGraddle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.mohit.firebase"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.firebase:firebase-client-android:2.5.2+'
}

i checked my firebase url. It's Perfectly Correct.

Tripedal answered 27/5, 2016 at 7:41 Comment(2)
Can you post a sample of your database structure? Also, are you implementing security rules by chance?Pourparler
Currently the default rules in firebase allows the read and write operations only to authorized uses.Lizbeth
L
100

It's because you are not authorized to the Database, check the Rules Tab in the Realtime database

If it's

{
  "rules": {
    ".read": "auth != null",
    ".write":"auth != null"
  }
}

This means only authorized user's can write and read the Data.

Changing to

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

Allows anyone to write the Database

When going for Production be sure to use the first one

Lugger answered 27/5, 2016 at 7:52 Comment(6)
May be it is not the best solution for the security.Lizbeth
Yes, but can be used for testing purposesLugger
Thanks. Its Working now. I was not authorized.Tripedal
@VeereshCharantimath I want to scan the database while registering a user to check if a certain 'username' is available or taken. How can I do this? Your above given solution might be handy in testing phase but what when publishing the app?Intersidereal
{ "rules": { ".read": "auth != null", ".write":"auth != null" } } How can I use this? I want to make it private to logged in users onlySheers
I have solved this issue using this tutorial. Thanks "youtu.be/b2SBkUWLKSc"Horde
B
-2

Listen at /child failed: FirebaseError: Permission denied

It means that the node "child" does not have access permission.

For a more secure approach, define the rule as follows:

{

 "rules": { 

"child": {

    ".read": "auth != null && auth.uid !=null" ,

    ".write": "auth != null && auth.uid !=null"  
   }

}
Blackguard answered 2/3, 2019 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.