权限被拒绝,在firebase中检索数据

时间:2017-05-12 10:54:04

标签: android firebase firebase-realtime-database

我尝试与特定所有者一起阅读我的所有设备信息,并提出一条规则,即如果用户是所有者,则用户只能获取设备信息。

所以我在Firebase中设置了这样的规则

{
  "rules": {   
     "devices": { 
        "$devices": {
            ".read": "data.child('owner').val() === auth.uid",          
            ".write": "auth != null"
        }
    }
}

例如我在firebase中的数据库是这样的:

{
  "devices" : {
    "SN1" : {

      "owner" : "Pl2YaoakaoPLeOowwm8RSFE5",
       [...]
    },
    "SN2" : {

      "owner" : "Pl2YaoakaoPLeOowwm8RSFE5",
      [...]
    },
    "SN3" : {

      "owner" : "KL2YaoaploPLeOoabm8KDFE4",
      [...]
    }
  }
}

我试图通过这样做来获取这些信息:

FirebaseUser user = firebaseAuth.getCurrentUser();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://NAME.firebaseio.com/");
        rootRef.child("devices").orderByChild("owner").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                System.out.println(snapshot.getValue());
                Log.v("TAG", "value Device : " + snapshot.getValue());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.v("TAG","The read failed: " + databaseError.getMessage());
            }
        });

我应该期待SN1和SN2设备的信息,但我获得了许可被拒绝: 侦听/设备失败:DatabaseError:权限被拒绝。
似乎他正在尝试获取所有设备(规则不允许),然后在本地执行过滤器 如果我直接检索这样的SN1数据:rootRef.child("devices").child("SN1"),它就可以正常工作。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

简单易读这些规则。!

  {
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".write": "$user_id === auth.uid"
      }
    }
  }
}


{
  "rules": {
    "users": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid"
      }
    }
  }
}

并在Fire base Thread

 private FirebaseAuth mAuth;
 public String id;

            mAuth = FirebaseAuth.getInstance();
            DatabaseReference rootRef = 
            FirebaseDatabase.getInstance().getReference();

             id = mAuth.getCurrentUser().getUid();


     rootRef.child("devices").addValueEventListener(new ValueEventListener() 
     {
     @Override
     public void onDataChange(DataSnapshot snapshot) {


     for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

     String key=dataSnapshot.getKey();






   rootRef.child("devices").child(key).orderByChild("owner")
   .equalTo(id).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {

     System.out.println(snapshot.getValue());
     Log.v("TAG", "value Device : " + snapshot.getValue());
                    }//end of for each loop
    }//end of data change

     @Override
     public void onCancelled(DatabaseError databaseError) {
                        Log.v("TAG","The read failed: " + 
     databaseError.getMessage());
                    }
                });

答案 1 :(得分:-2)

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

试试这些。