简体   繁体   English

数据库错误:权限被拒绝 Firebase

[英]DatabaseError: Permission denied firebase

I am new to firebase.我是火力基地的新手。 I want to read/write data with firebase in my app but when I try to do so I am getting this DatabaseError: Permission denied error.我想在我的应用程序中使用 firebase 读取/写入数据,但是当我尝试这样做时,我收到此DatabaseError: Permission denied错误。

Rules file on firebase Firebase 上的规则文件

{

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

Code file代码文件

        Firebase authFirebaseRef = new Firebase(ApiConstants.FIREBASE_MY_URL);
        authFirebaseRef.authWithCustomToken(AppConstants.FBT, authResultHandler);

Firebase.AuthResultHandler authResultHandler = new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        Log.v("onAuthenticated", authData.getUid());
        addEvents();
    }

    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        Log.v("onAuthenticationError= ", "FirebaseError= " + firebaseError.toString());
    }
};

private void addEvents() {

    mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservices");
    mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            emergencyServicesList.clear();
            for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
                Log.v("onDataChange", ":" + dataSnapshotChild.child("emergencyServicesName").getValue());
                EmergencyServiceModel emergencyServiceModel = new EmergencyServiceModel();
                emergencyServiceModel.setEmergencyServiceId(dataSnapshotChild.child("emergencyServicesId").getValue().toString());
                emergencyServiceModel.setEmergencyServiceName(dataSnapshotChild.child("emergencyServicesName").getValue().toString());
                emergencyServicesList.add(emergencyServiceModel);
            }
            loadDataToRecyclerView();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.v("DatabaseError", databaseError.getMessage());
        }
    });
}

Earlier I added few records in sos/emergencyservices by keeping .true to read/write .早些时候,我通过将.true保持为read/writesos/emergencyservices添加了一些记录。 But now I have to change it with authentication.但是现在我必须通过身份验证来更改它。

I think you're using different firebase object for auth and a different one for query.我认为您使用不同的 firebase 对象进行身份验证,使用不同的 firebase 对象进行查询。
Try this尝试这个

authFirebaseRef.getRoot().child("sos/emergencyservices"‌​).addValueEventListe‌​ner(new ValueEventListener() { ... }; 

instead of代替

mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservic‌​es");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() { ... };

Also, you are using the old version of firebase API.此外,您使用的是旧版本的 firebase API。 I refer you this link .我向你推荐这个链接 The document guides you through upgrading your existing Firebase.com app to the new Firebase console and APIs.该文档将指导您将现有的 Firebase.com 应用升级到新的 Firebase 控制台和 API。

We have one more source of the "Permission denied" errors, as of end of 2018.截至 2018 年底,我们还有一个“权限被拒绝”错误的来源。

Google has just introduces Cloud Firestore BETA. Google 刚刚推出了 Cloud Firestore BETA。

The point is it's interface is the same as for Firebase, there is only one dropdown selector to separate both databases ...关键是它的界面与 Firebase 相同,只有一个下拉选择器来分隔两个数据库......

Have spent 8h faiting with error, finally have found the default console view is a Cloud Firestore BETA view but not Firebase.花了 8 小时的错误,终于发现默认的控制台视图是 Cloud Firestore BETA 视图而不是 Firebase。

So my app talked to Firebase while I was configuring Cloud Firestore BETA.因此,当我配置 Cloud Firestore BETA 时,我的应用会与 Firebase 通信。

8h lost .... Thank you Google ... 8h 迷路了……谢谢谷歌……

You need to ensure the user is signed in before attaching the listener.在附加侦听器之前,您需要确保用户已登录。 Since you're already listening for the authentication state, it's a matter of moving the code that attaches the listener into the onAuthenticated callback:由于您已经在侦听身份验证状态,因此只需将附加侦听器的代码移动到onAuthenticated回调中即可:

Firebase.AuthResultHandler authResultHandler = new Firebase.AuthResultHandler() {
    @Override
    public void onAuthenticated(AuthData authData) {
        Log.v("onAuthenticated", authData.getUid());
        addEvents();

        mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservices");
        mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                emergencyServicesList.clear();
                for (DataSnapshot dataSnapshotChild : dataSnapshot.getChildren()) {
                    Log.v("onDataChange", ":" + dataSnapshotChild.child("emergencyServicesName").getValue());
                    EmergencyServiceModel emergencyServiceModel = new EmergencyServiceModel();
                    emergencyServiceModel.setEmergencyServiceId(dataSnapshotChild.child("emergencyServicesId").getValue().toString());
                    emergencyServiceModel.setEmergencyServiceName(dataSnapshotChild.child("emergencyServicesName").getValue().toString());
                    emergencyServicesList.add(emergencyServiceModel);
                }
                loadDataToRecyclerView();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.v("DatabaseError", databaseError.getMessage());
            }
        });
    }

    @Override
    public void onAuthenticationError(FirebaseError firebaseError) {
        Log.v("onAuthenticationError= ", "FirebaseError= " + firebaseError.toString());
    }
};

Well, did you enable the required sign-in method in firebase console?那么,您是否在 firebase 控制台中启用了所需的登录方法

firebase_signin_method

它与数据库有关,因此在 firebase 控制台中进入数据库并将规则设置为 true 以进行读/写

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

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