Firestore:拒绝授予权限的交易 [英] Firestore: Transactions giving permission denied

查看:275
本文介绍了Firestore:拒绝授予权限的交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在从我们的Java应用引擎实例访问 Firestore

非事务性请求成功,但事务失败,出现以下错误:
firestore:PERMISSION_DENIED:缺少权限或权限不足



示例交易

  final long updatedValue = 15; 

Firestore db = firebaseManager.getFirestore();
CollectionReference fooCollectionRef = db.collection(SOME_COLLECTION);
DocumentReference fooDocumentRef = fooCollectionRef.document(fooId);

最终ApiFuture< Long> future = db.runTransaction(transaction - > {
DocumentSnapshot snapshot = transaction.get(fooDocumentRef).get();
transaction.update(fooDocumentRef,SOME_FIELD,updatedValue);
return updatedValue ;
});
return future.get();

据我所知,我们对开发项目的权限完全打开:

  service cloud.firestore {
match / databases / {database} / documents {
match / {document = ** } {
允许读取,写入;



$ / code $ / pre

从我读过的,允许写入 应该允许事务更新正常工作,但他们和sime get 都不起作用。

任何人都知道我是否缺少一个步骤/设置了不正确的权限?



(我已经看到其他类似的问题发布了有关Firestore权限的错误,但在那些)

解决方案

我遇到了同样的问题,但我设法解决了这个问题。设置一个服务帐户并将您的Firestore实例指向该服务帐户。您可以在这里创建一个服务帐户密钥 。以下是显示如何使用服务帐户的示例代码块。 SERVICE_ACCOUNT_PATH是一个字符串,其中包含服务帐户的路径,PROJECT_ID是包含您项目ID的字符串。

  private static void authenticate ()抛出异常{
FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(SERVICE_ACCOUNT_PATH)))
.setProjectId(PROJECT_ID).build();

database = firestoreOptions.getService();
}


We are accessing Firestore from our Java app engine instance.

Non-transactional requests are succeeding fine, but transactions are failing with the error: firestore: PERMISSION_DENIED: Missing or insufficient permissions

Example Transaction

final long updatedValue = 15;

Firestore db = firebaseManager.getFirestore();
CollectionReference fooCollectionRef = db.collection(SOME_COLLECTION);
DocumentReference fooDocumentRef = fooCollectionRef.document(fooId);

final ApiFuture<Long> future = db.runTransaction(transaction -> {
    DocumentSnapshot snapshot = transaction.get(fooDocumentRef).get();
    transaction.update(fooDocumentRef, SOME_FIELD, updatedValue);
    return updatedValue;
});
return future.get();

As far as I can tell, our permissions on the dev project are completely open:

service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read, write;
        }
    }
}

From what I have read, allowing writes should also allow transaction updates to work, but neither they nor sime get calls are working.

Does anyone know if I am missing a step/have incorrect permissions set up?

(I have seen other similar questions posted about Firestore permission denied errors, but in those cases the rules are more specific. Currently, this project does not even require auth to access Firestore.)

解决方案

I was running into this same issue, but I managed to fix it. Set up a service account and point your Firestore instance to that. You can create a service account key here. Here's a sample code block showing how to use the service account. SERVICE_ACCOUNT_PATH is a string that contains the path to the service account and PROJECT_ID is a string containing your project id.

private static void authenticate() throws Exception {
     FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
          .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(SERVICE_ACCOUNT_PATH)))
          .setProjectId(PROJECT_ID).build();

     database = firestoreOptions.getService();
}

这篇关于Firestore:拒绝授予权限的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆