从 GAE 开发服务器使用 Firestore 时出现未经验证的异常

UNAUTHENTICATED exception when using Firestore from GAE development server

我正在制作一个 Android 应用程序,该应用程序将 Cloud Endpoints Framework 用于后端 API。我正在使用 App Engine 开发服务器测试后端。在其中一个后端函数中,我尝试调用以从 Firestore 数据库获取文档,但出现此错误:

com.google.api.server.spi.SystemService invokeServiceMethod SEVERE: exception occurred while calling backend method java.util.concurrent.ExecutionException: io.grpc.StatusRuntimeException: UNAUTHENTICATED 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:57) ...

我相信我已经正确设置了服务帐户。我将环境变量 GOOGL_APPLICATION_CREDENTIALS 设置为服务帐户密钥的路径。我也在遵循实例化和调用 Firestore 的指南:

import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
FirestoreOptions firestoreOptions =
FirestoreOptions.getDefaultInstance().toBuilder()
   .setProjectId(projectId)
   .build();
Firestore db = firestoreOptions.getService();
db.collection(collectionId).get().get();

我是否缺少能够从 GAE 调用 Firestore 的东西?

在与这个问题斗争了一段时间之后,我偶然发现了一个临时解决方法。似乎数据库确实尚未经过身份验证,在获取服务后添加调用以获取请求元数据似乎触发了身份验证...

Firestore db = firestoreOptions.getService();

try {
  // somehow without this it does not work on the local server
  db.getOptions().getCredentials().getRequestMetadata();
} catch (IOException e) {
  e.printStackTrace();
}

... // db access works on local server!