Android Firebase setValue()权限被拒绝 [英] Android Firebase setValue() Permission Denied

查看:171
本文介绍了Android Firebase setValue()权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是规则在firebase上的定义:

  {
rules:{
users:{
.read:true,
$ user_id:{
.write:auth.uid === $ user_id,
.read:true
}}}}

信息在我的注册活动使用setValue()就像下面,并添加新的评论使用push()。setValue(),每次我没有认证问题。直接添加评论的 http:// DB / users / 新用户。



下面是代码snippit,我的代码碰到了Log:数据无法保存。每一次。

  AuthData authData = newRef.getAuth(); (authData!= null){
Log.v(LOG_TAG,Auth as+ authData.getUid());


地图< String,Object> newEntry = new HashMap< String,Object>();
newEntry.put(Name,name);
newEntry.put(Description,desc);
newEntry.put(DueDate,date);
newEntry.put(Status,0);
$ b $ newRef.setValue(newEntry,new Firebase.CompletionListener(){
@Override $ b $ public void onComplete(FirebaseError firebaseError,Firebase firebase){
if(firebaseError! = null){
Log.v(LOG_TAG,Data can not be saved。+ firebaseError.getMessage()); //每次点击这里。 v(LOG_TAG,Data saved successfully。Finishing activity ...);
finish();
}
}
});
;
}
else {
Log.v(LOG_TAG,No authdata);
}

希望我提供了足够的信息,我猜这个问题是安全/规则,但任何方向/指导将非常感激。谢谢。

解决方案

我太困扰这一点,这是对我的帮助。首先,有两种类型的用户可以从Firebase访问数据库。


  1. 授权

  2. 非授权

默认情况下,它被设置为非授权,但是它们既没有任何权限阅读写入,所以最初如果您尝试执行任何操作,您将获得权限被拒绝错误



如何改变这个以符合您的要求?

解决方案:

在数据库部分下,转到规则选项卡。网址将与您的项目名称一样。

https:/ /console.firebase.google.com/project/project-name/database/rules



您会在这里看到类似这样的内容:

  {
rules:{
.read:auth!= null,//未经授权用户CANT读取
.write:auth!= null//非授权用户CANT写入
}
}

只要将它改为

  {
rules :{
.read:auth == null,//甚至非授权用户都可以读取
.write:auth == null//甚至非授权用户可以写


code
$ b

按照你的要求更改。快乐编码:)

This is how the rules are defined on firebase:

{
"rules": {
 "users": {
  ".read": true,
    "$user_id": {
     ".write": "auth.uid === $user_id",
     ".read" : true
  }}}}

I have succesfully written new user information in my register activity using setValue() just like below, and also added new comments using push().setValue(), each time I didn't have an issue with authentication. New user written straight to http://DB/users/ while comments were added very deep.

Here is a code snippit, my code hits the Log: Data could not be saved. Everytime.

AuthData authData = newRef.getAuth();

    if (authData != null) {
        Log.v(LOG_TAG, "Auth as " + authData.getUid());
        Map<String, Object> newEntry = new HashMap<String, Object>();
        newEntry.put("Name", name);
        newEntry.put("Description", desc);
        newEntry.put("DueDate", date);
        newEntry.put("Status", 0);

        newRef.setValue(newEntry, new Firebase.CompletionListener() {
            @Override
            public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                if (firebaseError != null) {
                    Log.v(LOG_TAG, "Data could not be saved. " + firebaseError.getMessage()); //Hits here every time.
                } else {
                    Log.v(LOG_TAG, "Data saved successfully. Finishing activity...");
                    finish();
                }
            }
        });
        ;
    }
    else{
        Log.v(LOG_TAG, "No authdata");
    }

Hopefully I've provided enough information, I'm guessing the issue is in the security/rules but any direction/guidance would be very appreciated. Thank you.

解决方案

I was too stuck on this point and here's what helped me.

First things first, there are two types of users who can access database from firebase

  1. Authorized
  2. Non-authorized

By default it is set to non-authorized but then they do not have any permissions neither read nor write, so initially if you try to perform any operation you get the permission denied error.

How to change this to suit your requirement?

Solution:

Under Database section, go to Rules tab. The URL will be something like this with your project name.
https://console.firebase.google.com/project/project-name/database/rules

You will see somthing like this here:

{
  "rules": {
    ".read": "auth != null", //non-authorised users CANT read
    ".write": "auth != null" //non-authorised users CANT write
  }
}

Just change this to

{
  "rules": {
    ".read": "auth == null", //even non-authorised users CAN read
    ".write": "auth == null" //even non-authorised users CAN write
  }
}

Change this as per your requirement. Happy coding :)

这篇关于Android Firebase setValue()权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆