Search code examples
androidgoogle-app-enginegoogle-cloud-endpointsgoogle-cloud-firestore

UNAUTHENTICATED exception when using Firestore from GAE development server


I'm making an Android app that uses Cloud Endpoints Framework for the backend API. I'm testing the backend using the App Engine development server. In one of the backend functions I try to make a call to get a document from a Firestore database but I get this error:

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) ...

I believe that I have the service account setup properly. I have the environment variable GOOGL_APPLICATION_CREDENTIALS set to the path of the service account key. I'm also following the guide for instantiating and calling 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();

Is there something I'm missing here to be able to call Firestore from GAE?


Solution

  • After fighting with this issue for some time, I incidentally found a temporary workaround. It seems that the db is indeed not authenticated yet and adding a call after getting the service to get the request meta data seems to trigger the authentification...

    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!