1

我是firebase的新手。我想在我的應用程序中使用firebase讀取/寫入數據,但是當我嘗試這樣做時,出現DatabaseError: Permission denied錯誤。DatabaseError:權限被拒絕firebase

規則上的火力點

{ 

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

代碼文件文件

 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()); 
     } 
    }); 
} 

早些時候,我在sos/emergencyservices添加一些記錄通過保持.trueread/write。但現在我必須通過認證來改變它。

+0

檢查此鏈接https://stackoverflow.com/questions/37424544/firebase-database-permission-denied-with-read-write-permissions-set-to-true –

+0

你啓用從控制檯登錄? –

+0

@akshay_shahane是的,我已經做過 – VVB

回答

1

我覺得您使用的身份驗證不同火力的對象和不同的一個查詢。
試試這個

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

代替

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

此外,您使用的是舊版本的火力點的API。我向你推薦link。本文檔將指導您將現有的Firebase.com應用升級到新的Firebase控制檯和API。

+0

它在你的回答中編輯幾件東西 – VVB

+0

請仔細閱讀文檔。升級到新的消防基地將使您的工作更加輕鬆,高效,並且在技術上更新。而且,你也會發現很多資源。 – Journey

+0

好的。再一次感謝你 – VVB

0

您需要確保用戶在連接偵聽器之前登錄。既然你已經聽的認證狀態,它的移動,重視聽衆進入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()); 
    } 
}; 
+0

謝謝你的回覆。是的,我也這樣做。請參閱編輯代碼 – VVB

+0

嗯....不知道發生了什麼。錯誤消息應該告訴你它失敗的位置。 –

+0

@VVB它有效嗎? – Journey

0

好吧,你啓用火力控制檯所需的登錄方法

firebase_signin_method

+0

是的,我已經做到了 – VVB

+0

您是否設法註冊用戶? – Journey

+0

是的,我使用自定義的JWT firebase集成做到了 – VVB