Kuro
Kuro

Reputation: 321

How to pass the credential to Translation API v3?

I am using Google Cloud Translate v3 API Java Client. The Java Client code samples work great. The authentication of the code samples is done using the GOOGLE_APPLICATION_CREDENTIALS environment variable.

I need to develop code that will run in an application server that I have no control over the environment variables. So I need to use another way to get the call authenticated. Setting Up Authentication for Server to Server Production Applications, under "Passing the path to the service account key in code" has a code sample (choose the JAVA tab) that works for the Storage service, but I can't find a similar way to pass the GoogleCredentials to Translation v3 API Java Client. Does anyone know?

Also, I can't find the Java doc for v3 API. https://googleapis.dev/java/google-cloud-clients/latest/index.html shows version "0.119.0-alpha" and it does not list the package com.google.cloud.translate.v3. Is there a separate java doc page ?

Upvotes: 7

Views: 2275

Answers (1)

Kuro
Kuro

Reputation: 321

I think I found a solution. The client API javadoc doesn't list com.google.cloud.translate.v3 but it does list com.google.cloud.translate.v3beta1. In the javadoc for TranslationServiceClient, i.e. https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/translate/v3beta1/TranslationServiceClient.html there's a mention of setting credential, and this method worked!

 TranslationServiceSettings translationServiceSettings =
     TranslationServiceSettings.newBuilder()
         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
         .build();
 TranslationServiceClient translationServiceClient =
     TranslationServiceClient.create(translationServiceSettings);

Upvotes: 5

Related Questions