Tuesday, May 29, 2018

Translation API Client Libraries (Java)

Installing the client library

Setting up authentication

To run the client library, you must first set up authentication by creating a service account and setting an environment variable.
GCP Console


  1. Go to the Create service account key page in the GCP Console.
    Go to the Create Service Account Key page
  2. From the Service account drop-down list, select New service account.
  3. Enter a name into the Service account name field.
  4. From the Role drop-down list, select Project > Owner.
    Note: The Role field authorizes your service account to access resources. You can view and change this field later using GCP Console. If you are developing a production application, specify more granular permissions than Project > Owner. For more information, see granting roles to service accounts.
  5. Click Create. A JSON file that contains your key downloads to your computer.

Enviroment Variables
Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. Replace [PATH] with the file path of the JSON file that contains your service account key, and [FILE_NAME] with the filename.

Clone source
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
cd ./java-docs-samples/translate/cloud-client

Build your project with:
mvn clean package -DskipTests
You can then run a given ClassName via:
mvn exec:java -Dexec.mainClass=com.example.translate.ClassName \
    -DpropertyName=propertyValue \
 -Dexec.args="any arguments to the app"

Translate a string (using the quickstart sample)

mvn exec:java -Dexec.mainClass=com.example.translate.QuickstartSample

No comments:

Post a Comment