google-app-engine - google app engine - How to fix error: A project ID is required for this service but could not be determined - answerstu - answerstu.com answerstu

google app engine - How to fix error: A project ID is required for this service but could not be determined

I'm trying to insert data to Google Datastore from AppEngine and I'm getting an error:

java.lang.IllegalArgumentException: A project ID is required for this service but could not be determined from the builder or the environment.  Please set a project ID using the builder.
    at com.google.common.base.Preconditions.checkArgument(Preconditions.java:92)
    at com.google.cloud.ServiceOptions.<init>(ServiceOptions.java:324)
    at com.google.cloud.datastore.DatastoreOptions.<init>(DatastoreOptions.java:85)
    at com.google.cloud.datastore.DatastoreOptions.<init>(DatastoreOptions.java:32)
    at com.google.cloud.datastore.DatastoreOptions$Builder.build(DatastoreOptions.java:75)
    at com.google.cloud.datastore.DatastoreOptions.defaultInstance(DatastoreOptions.java:123)

Here's my code:

Datastore datastore = DatastoreOptions.defaultInstance().service();     
             KeyFactory keyFactory = datastore.newKeyFactory().kind("keyKind"); 
             Key key = keyFactory.newKey("keyName"); 
             Entity entity = Entity.builder(key) 
                 .set("name", "John Doe") 
                 .set("age", 30) 
                 .set("access_time", DateTime.now()) 
                 .build(); 
             datastore.put(entity); 

How do I fix this error?

2 Answers

  1. Patrick- Reply

    2019-11-14

    Are you running AE standard or AE Flexible?

    Make sure you have the App Engine api jar in your classpath (WEB-INF/lib directory).

  2. Peter- Reply

    2019-11-14

    In my case the next code is working:

        BigQuery bigquery = BigQueryOptions.newBuilder().setProjectId("XXXXX")
                .setCredentials(
                        ServiceAccountCredentials.fromStream(new FileInputStream("key.json"))
                ).build().getService();
    

    I set the projectId in the builder.

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>