Question: Google API authentication in Java without using environment variable

Question

Google API authentication in Java without using environment variable

Answers 1
Added at 2017-01-05 11:01
Tags
Question

I have set up a simple test app to interact with Google's Natural Language API. I created a service account, and downloaded the JSON credentials. I am running on a local development machine, so I set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the JSON file. To be clear, this works: the app successfully makes some API calls and displays the results.

I would like to remove the dependence on the environment variable. How can I use the known location of the JSON file (or any other approach) in the application to create the LanguageServiceClient with those credentials?

Answers to

Google API authentication in Java without using environment variable

nr: #1 dodano: 2017-01-07 22:01

You can register is like this:

DatastoreOptions options = DatastoreOptions.newBuilder()
  .setProjectId(PROJECT_ID)
  .setAuthCredentials(AuthCredentials.createForJson(
    new FileInputStream(PATH_TO_JSON_KEY))).build();

Does that help?

Source Show
◀ Wstecz