Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google API authentication in Java without using environment variable

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?

like image 204
Paul A. Hoadley Avatar asked Jan 05 '17 10:01

Paul A. Hoadley


3 Answers

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?

like image 108
Jochen Bedersdorfer Avatar answered Nov 14 '22 19:11

Jochen Bedersdorfer


You can always pass the full json file as String as follows:

        CredentialsProvider credentialsProvider;
        String credentials = "[YOUR JSON FILE CONTENT]";

        try {

            credentialsProvider
                    = FixedCredentialsProvider.create(
                            ServiceAccountCredentials.fromStream(new ByteArrayInputStream(credentials.getBytes())));          

        } catch (IOException ex) {
            Logger.getLogger(GoogleNLPService.class.getName()).log(Level.SEVERE, null, ex);
        }

        LanguageServiceSettings.Builder languageServiceSettingsBuilder
                = LanguageServiceSettings.newBuilder();

        LanguageServiceSettings languageServiceSettings = languageServiceSettingsBuilder.setCredentialsProvider(credentialsProvider).build();

        List<NamedEntity> entities = new ArrayList<>();
        try (LanguageServiceClient language = LanguageServiceClient.create(languageServiceSettings)) {

         ...

        }

Alternatively, you can place your json file in resources folder and then read it as:

    credentialsProvider
            = FixedCredentialsProvider.create(
                    ServiceAccountCredentials.fromStream(new FileInputStream("./src/main/resources/FILENAME.json")));

However, this relative path didn't work when I uploaded my app in Heroku. So, I have decided to use the String solution.

like image 3
stzoannos Avatar answered Nov 14 '22 18:11

stzoannos


We use a service account + GoogleCredential.Builder -- (note that this example uses a credential file in p12 format); example follows:

 private GoogleCredential authorize() throws IOException, GeneralSecurityException
{
    return new GoogleCredential.Builder()
    .setTransport(HTTP_TRANSPORT)
    .setJsonFactory(JSON_FACTORY)
    .setServiceAccountId(serviceAccount)
    .setServiceAccountScopes(SCOPES)
    .setServiceAccountUser(serviceAccountUser)
    // variable p12File is a String w/ path to the .p12 file name
    .setServiceAccountPrivateKeyFromP12File(new java.io.File(p12File))
      .build();
}
like image 2
Steve Harrington Avatar answered Nov 14 '22 17:11

Steve Harrington