简体   繁体   English

Android Firebase setValue()权限被拒绝

[英]Android Firebase setValue() Permission Denied

This is how the rules are defined on firebase: 这是在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. 我已经使用setValue()成功地在我的注册活动中编写了新的用户信息,就像下面一样,并且每次我都没有使用put()。setValue()时添加了新的注释。 New user written straight to http://DB/users/ while comments were added very deep. 新用户直接写入http:// DB / users /,同时添加了非常深入的评论。

Here is a code snippit, my code hits the Log: Data could not be saved. 这是一个代码snippit,我的代码命中日志:数据无法保存。 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 首先,有两种类型的用户可以从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. 在Database部分下,转到Rules选项卡。 The URL will be something like this with your project name. URL将与您的项目名称类似。
https://console.firebase.google.com/project/project-name/database/rules 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 :) 快乐编码:)

For people who have this problem, do not forget to change the "type of database" that by default gives us firebase, it should be noted that there is 2 type of database one that is Cloud Firestore and another that is RealTime Database 对于有此问题的人,不要忘记更改默认情况下为我们提供firebase的“数据库类型”,应该注意的是有两种类型的数据库,一种是Cloud Firestore ,另一种是RealTime数据库。

To answer the question, make sure you are using Realtime database: 要回答这个问题,请确保您使用的是Realtime数据库:

更改数据库的类型

Then edit the rules: 然后编辑规则:

在“数据库”>“规则”部分中编辑安全规则

Obviously this configuration of rules should only be used for development environments, do not forget to change the rules when it is in production. 显然,这种规则配置只应用于开发环境,不要忘记在生产中更改规则。

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

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