0

I am trying to connect to Google Cloud Datastore, I am following this tutorial :

https://cloud.google.com/datastore/docs/getstarted/start_java/

I am managing to connect and retrieve all the informations I want but I am connecting with options from environment like so :

datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv() .dataset(datasetId).build());

Now I am trying to provide the options programmatically. As I understood I can do it by modifying the credentials like so :

datastore = DatastoreFactory.get() .create(DatastoreHelper .getOptionsfromEnv() .credential(credential) .dataset(datasetId).build());

Here is how I declare my credentials :

String filename = "XXX/src/main/webapp/WEB-INF/XXX9f29f.p12";

    credential = new GoogleCredential.Builder()
                        .setTransport(HTTP_TRANSPORT)
                        .setJsonFactory(JSON_FACTORY)
                        .setServiceAccountId("XXXX@developer.gserviceaccount.com")
                        .setServiceAccountPrivateKeyFromP12File(new File(filename)).build();

My problem is that I get a java.lang.NullPointerException from this line :

.setServiceAccountPrivateKeyFromP12File(new File(filename))

My application has read/write access to the file, which is located in the webapp/WEB-INF folder.

Here is my log output :

GRAVE: Unhandled exception
java.lang.NullPointerException
    at java.util.Collections$UnmodifiableCollection.<init>(Collections.java:1026)
    at java.util.Collections.unmodifiableCollection(Collections.java:1013)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.<init>(GoogleCredential.java:208)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential$Builder.build(GoogleCredential.java:368)
    at com.databerries.bidder.DataBaseHandler.setEnv(DataBaseHandler.java:78)
    at com.databerries.bidder.WebServer.lambda$start$1(WebServer.java:75)
    ...
    at java.lang.Thread.run(Thread.java:745)

Thanks for any help :)

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Théo Richard
  • 247
  • 3
  • 12

1 Answers1

1

My problem came from the way I created my credential object.

Instead of creating it from scracth, I now get it from a DatastoreHelper like so :

credential = DatastoreHelper.getServiceAccountCredential(serviceAccount, filename);

Théo Richard
  • 247
  • 3
  • 12