Search code examples
firebasegoogle-app-enginegoogle-cloud-firestoregoogle-cloud-datastore

GAE - No API environment is registered for this thread


I have an spring-boot application deployed to google cloud using google app engine. There is a datastore with multiple entities. When I try to retrieve data through my app (app is uploaded to appengine, not locally) it always throws an error -

There was an unexpected error (type=Internal Server Error, status=500). No API environment is registered for this thread.

I am trying to make it work this way:

@Bean
public FirebaseAuth firebaseAuth() throws IOException {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream
        serviceAccount = classLoader.getResourceAsStream("downloaded-key-270620-2a16da8f4062.json");
    FirebaseOptions options = (new Builder())
                                  .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                                  .setDatabaseUrl(
                                      "https://my-app.firebaseio.com")
                                  .build();
    FirebaseApp.initializeApp(options);
    return FirebaseAuth.getInstance();
}

private Result<Product> list(boolean wholesale, String startCursorString) {
    FetchOptions fetchOptions = Builder.withLimit(10);
    if (startCursorString != null && !startCursorString.equals("")) {
        fetchOptions.startCursor(Cursor.fromWebSafeString(startCursorString));
    }

    Query query = (new Query("Product")).setFilter(
        new FilterPredicate("wholesale", FilterOperator.EQUAL, wholesale))
                                        .addSort("name", SortDirection.ASCENDING);
    PreparedQuery preparedQuery = this.datastore.prepare(query);
    QueryResultIterator<Entity> results = preparedQuery.asQueryResultIterator(fetchOptions);
    List<Product> resultBooks = this.entitiesToObjects(results);
    Cursor cursor = results.getCursor();
    if (cursor != null && resultBooks.size() == 10) {
        String cursorString = cursor.toWebSafeString();
        return new Result(resultBooks, cursorString);
    } else {
        return new Result(resultBooks);
    }
}

public void configure(WebSecurity web) throws Exception {
    web.ignoring()
       .antMatchers(HttpMethod.GET, new String[] { "/products" })
       .antMatchers(HttpMethod.GET, new String[] { "/purchases/update" })
       .antMatchers(HttpMethod.GET, new String[] { "/purchases/statistics/**" });
}

Any idea what could be the problem?


Solution

  • I think you might be missing inizialiting it according to the documentation, it would be something like:

    Datastore datastore = DatastoreOptions.getDefaultInstance().getService();