INTERMITTENT FirebaseFirestoreException:PERMISSION_DENIED:缺少权限或权限不足

时间:2019-03-28 12:30:05

标签: android firebase google-cloud-firestore firebase-security-rules

我有一个带有PeriodicWork的android应用,该应用每30分钟将数据写入Firestore。它工作正常,但我注意到在某些情况下,它会失败,但出现FirebaseFirestoreException: PERMISSION_DENIED例外。我不明白,如果它可以将数据写入Firestore,为什么会抱怨该权限。

// Firestore Rules
service cloud.firestore {
  match /databases/{database}/documents {

    match /bar/{document=**} {
      allow read: if true;
      allow create: if true;
    }

  }
}
// Write to Firestore
val firebaseFirestore = FirebaseFirestore.getInstance()

firebaseFirestore.collection(BAR_KEY)
        .add(barData)
        .addOnSuccessListener { /*success callback*/ }
        .addOnFailureListener { e ->
            Crashlytics.log(e.message)
            Crashlytics.logException(e)
            /* failure callback */
        }
Non-fatal Exception: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
       at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@18.1.0:119)
       at com.google.firebase.firestore.core.SyncEngine.notifyUser(com.google.firebase:firebase-firestore@@18.1.0:446)
       at com.google.firebase.firestore.core.SyncEngine.handleRejectedWrite(com.google.firebase:firebase-firestore@@18.1.0:430)
       at com.google.firebase.firestore.core.FirestoreClient.handleRejectedWrite(com.google.firebase:firebase-firestore@@18.1.0:275)
       at com.google.firebase.firestore.remote.RemoteStore.handleWriteError(com.google.firebase:firebase-firestore@@18.1.0:707)
       at com.google.firebase.firestore.remote.RemoteStore.handleWriteStreamClose(com.google.firebase:firebase-firestore@@18.1.0:663)
       at com.google.firebase.firestore.remote.RemoteStore.access$600(com.google.firebase:firebase-firestore@@18.1.0:53)
       at com.google.firebase.firestore.remote.RemoteStore$2.onClose(com.google.firebase:firebase-firestore@@18.1.0:206)
       at com.google.firebase.firestore.remote.AbstractStream.close(com.google.firebase:firebase-firestore@@18.1.0:334)
       at com.google.firebase.firestore.remote.AbstractStream.handleServerClose(com.google.firebase:firebase-firestore@@18.1.0:388)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.lambda$onClose$3(com.google.firebase:firebase-firestore@@18.1.0:149)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver$$Lambda$4.run(Unknown Source:4)
       at com.google.firebase.firestore.remote.AbstractStream$CloseGuardedRunner.run(com.google.firebase:firebase-firestore@@18.1.0:67)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.onClose(com.google.firebase:firebase-firestore@@18.1.0:135)
       at com.google.firebase.firestore.util.FirestoreChannel$1.onClose(com.google.firebase:firebase-firestore@@18.1.0:161)
       at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
       at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
       at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
       at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:678)
       at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
       at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
       at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
       at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:397)
       at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:459)
       at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:546)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:467)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:584)
       at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
       at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at com.google.firebase.firestore.util.AsyncQueue$DelayedStartFactory.run(com.google.firebase:firebase-firestore@@18.1.0:205)
       at java.lang.Thread.run(Thread.java:764)
Caused by io.grpc.StatusException: PERMISSION_DENIED: Missing or insufficient permissions.
       at io.grpc.Status.asException(Status.java:534)
       at com.google.firebase.firestore.util.Util.exceptionFromStatus(com.google.firebase:firebase-firestore@@18.1.0:117)
       at com.google.firebase.firestore.core.SyncEngine.notifyUser(com.google.firebase:firebase-firestore@@18.1.0:446)
       at com.google.firebase.firestore.core.SyncEngine.handleRejectedWrite(com.google.firebase:firebase-firestore@@18.1.0:430)
       at com.google.firebase.firestore.core.FirestoreClient.handleRejectedWrite(com.google.firebase:firebase-firestore@@18.1.0:275)
       at com.google.firebase.firestore.remote.RemoteStore.handleWriteError(com.google.firebase:firebase-firestore@@18.1.0:707)
       at com.google.firebase.firestore.remote.RemoteStore.handleWriteStreamClose(com.google.firebase:firebase-firestore@@18.1.0:663)
       at com.google.firebase.firestore.remote.RemoteStore.access$600(com.google.firebase:firebase-firestore@@18.1.0:53)
       at com.google.firebase.firestore.remote.RemoteStore$2.onClose(com.google.firebase:firebase-firestore@@18.1.0:206)
       at com.google.firebase.firestore.remote.AbstractStream.close(com.google.firebase:firebase-firestore@@18.1.0:334)
       at com.google.firebase.firestore.remote.AbstractStream.handleServerClose(com.google.firebase:firebase-firestore@@18.1.0:388)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.lambda$onClose$3(com.google.firebase:firebase-firestore@@18.1.0:149)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver$$Lambda$4.run(Unknown Source:4)
       at com.google.firebase.firestore.remote.AbstractStream$CloseGuardedRunner.run(com.google.firebase:firebase-firestore@@18.1.0:67)
       at com.google.firebase.firestore.remote.AbstractStream$StreamObserver.onClose(com.google.firebase:firebase-firestore@@18.1.0:135)
       at com.google.firebase.firestore.util.FirestoreChannel$1.onClose(com.google.firebase:firebase-firestore@@18.1.0:161)
       at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
       at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
       at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
       at io.grpc.internal.CensusStatsModule$StatsClientInterceptor$1$1.onClose(CensusStatsModule.java:678)
       at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39)
       at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23)
       at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40)
       at io.grpc.internal.CensusTracingModule$TracingClientInterceptor$1$1.onClose(CensusTracingModule.java:397)
       at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:459)
       at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:63)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.close(ClientCallImpl.java:546)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.access$600(ClientCallImpl.java:467)
       at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:584)
       at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
       at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:123)
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at com.google.firebase.firestore.util.AsyncQueue$DelayedStartFactory.run(com.google.firebase:firebase-firestore@@18.1.0:205)
       at java.lang.Thread.run(Thread.java:764)

1 个答案:

答案 0 :(得分:0)

当您尝试再次更新同一文档时,它将失败。

将add(barData)更改为set(barData,SetOptions.merge())

并在安全规则中添加以下行

allow update: if true;