【问题标题】:Firestore PERMISSION_DENIED on Google Cloud ComputeGoogle Cloud Compute 上的 Firestore PERMISSION_DENIED
【发布时间】:2019-02-27 11:10:24
【问题描述】:

我有一个使用 firebase-admin 的 Java 应用程序,我正试图观察 Firestore 中的一个集合并根据对它的修改采取行动。

我有以下代码:

final GoogleCredentials credentials = GoogleCredentials
            .getApplicationDefault();

final FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(credentials).setProjectId("XXX")
            .build();

FirebaseApp.initializeApp(options);

Firestore db = FirestoreClient.getFirestore();

其中“XXX”是我的项目的三重检查 ID。

稍后会有这个:

final ApiFuture<QuerySnapshot> future = db.collection("YYY").get();
final List<QueryDocumentSnapshot> documents = future.get().getDocuments();

其中“YYY”是集合的名称(我尝试在空集合和非空集合上运行它)。

构建应用程序后,我将其上传到 Google Compute Engine 实例。当我运行它时,最后一行导致以下错误:

java.util.concurrent.ExecutionException: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Request had insufficient authentication scopes.
        at com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:500)
        at com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:479)
        at com.google.api.core.AbstractApiFuture.get(AbstractApiFuture.java:56)
        at com.dammitchat.App.main(App.java:55)

我在虚拟机上运行了以下命令:

~$ gcloud auth list
                  Credentialed Accounts
ACTIVE  ACCOUNT
*       XXXXXXXXXXXX-compute@developer.gserviceaccount.com

我检查了 IAM 并且此服务帐户具有“编辑”角色(以防万一,我也尝试添加“云数据存储所有者”),所以我想它可以绕过我为我的收藏设置的任何规则。事实上,这个集合有一条规则:

match /YYY/{y} {

    allow read: if false;
}

但即使我将其更改为true,结果还是一样。

现在,奇怪的是,当我使用另一个“云数据存储所有者”服务帐户在本地运行应用程序时,它工作正常。

我做错了什么?

【问题讨论】:

    标签: java firebase google-cloud-platform google-cloud-firestore firebase-admin


    【解决方案1】:

    上面写着:

    请求的身份验证范围不足。

    对于RPC 其中一个 需要请求这些 oAuth2 范围:

    解释了服务帐户身份验证here(这是错误消息提示的内容)。

    有关安全规则(不是该错误消息的原因),请参阅here

    另请参阅 Cloud Firestore API 和 Java example

    【讨论】:

    • 范围!感谢那。不知何故,我完全错过了这个概念。现在可以使用了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 2022-12-28
    • 1970-01-01
    相关资源
    最近更新 更多