我有火力數據庫,並將其工作正常,直到規則被設定爲這樣:權限匿名身份驗證拒絕火力地堡數據庫
"rules": {
".read": true,
".write": false
}
但現在我需要啓動我的應用程序,所以我不希望我的數據庫爲所有人開放。這就是爲什麼我改變數據庫規則:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
我從androd應用程式設定anonymouse身份驗證,但權限被拒絕的錯誤不斷出現。順便提一下,Anonymouse身份驗證可以在模擬器中正常工作。
這是我是如何實現它:
public void fireBaseAuthenticate(final Context context) {
mAuth = FirebaseAuth.getInstance();
mAuth.signInAnonymously()
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// debugger comes here, so Authentication is successful
} else {
// If sign in fails, display a message to the user.
}
}
});
}
public void load(final Context context) {
fireBaseAuthenticate(context);
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference();
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot categorySnaps : dataSnapshot.getChildren()) {
RetailChain retailChain = new RetailChain();
retailChain.setName(categorySnaps.getKey());
for (DataSnapshot subMenuSnaps : categorySnaps.getChildren()) {
for (DataSnapshot itemSnaps : subMenuSnaps.getChildren()) {
for (DataSnapshot productSnaps : itemSnaps.getChildren()) {
Product product = productSnaps.getValue(Product.class);
productList.add(product);
}
}
}
retailChain.setProducts(productList);
retailChainList.add(retailChain);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("database error: ", "onCancelled", databaseError.toException());
}
});
}
添加監聽singleValueEvent
後,OnCancelled
被調用。 那麼爲什麼權限被拒絕? 編輯: 錯誤日誌:
com.google.firebase.database.DatabaseException: Firebase Database error:
Permission denied
at com.google.firebase.database.DatabaseError.toException(Unknown Source)
at home.gio.calorieplanner.main.Main$3.onCancelled(Main.java:205)
at com.google.firebase.database.Query$1.onCancelled(Unknown Source)
at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
at com.google.android.gms.internal.zzbqv.zzZT(Unknown Source)
at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
請在文章中添加錯誤代碼,以便人們可以幫助您。 '許可被拒絕的錯誤持續顯示'是什麼意思? – UmarZaii
@ giusha9請檢查我的答案謝謝 – faruk