简体   繁体   English

Firebase权限被拒绝错误

[英]Firebase Permission denied Error

I am Very beginner to firebase and trying to get value from my database. 我是Firebase的初学者,正在尝试从数据库中获取价值。

but it showing me same error every time. 但是每次都显示相同的错误。

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

My firebase rules 我的firebase规则

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

my Androidmanifest.xml 我的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; 我的mainactivity.java包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 我的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. 我检查了我的firebase URL。 It's Perfectly Correct. 完全正确。

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 进行生产时,请务必使用第一个

Listen at /child failed: FirebaseError: Permission denied 侦听/ child失败:FirebaseError:权限被拒绝

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"  
   }

}

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

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