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.
Rules file on 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
. 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.
Try this
authFirebaseRef.getRoot().child("sos/emergencyservices").addValueEventListener(new ValueEventListener() { ... };
instead of
mFirebaseDatabase = mFirebaseInstance.getReference("sos").child("emergencyservices");
mFirebaseDatabase.addValueEventListener(new ValueEventListener() { ... };
Also, you are using the old version of 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.
We have one more source of the "Permission denied" errors, as of end of 2018.
Google has just introduces 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 ...
Have spent 8h faiting with error, finally have found the default console view is a Cloud Firestore BETA view but not Firebase.
So my app talked to Firebase while I was configuring Cloud Firestore BETA.
8h lost .... Thank you Google ...
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:
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());
}
};
它与数据库有关,因此在 firebase 控制台中进入数据库并将规则设置为 true 以进行读/写
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.