I follow the https://cloud.google.com/translate/docs/reference/libraries#client-libraries-usage-java to get started java client demo.
I have already set
authentication json file to the environment variable GOOGLE_APPLICATION_CREDENTIALS
. However, I got the translateException when I run java sample code.
Exception in thread "main" com.google.cloud.translate.TranslateException: The request is missing a valid API key.
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:144)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:113)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:110)
Caused by:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403
Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "The request is missing a valid API key.",
"reason" : "forbidden"
} ],
"message" : "The request is missing a valid API key.",
"status" : "PERMISSION_DENIED"
}
The doc shows that this JSON file contains key infomation.
The sample code is shown
// Instantiates a client
Translate translate = TranslateOptions.getDefaultInstance().getService();
// The text to translate
String text = "Hello, world!";
// Translates some text into Russian
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
I have no idea how to set api-key.
It still doesn't work after I set environment variable for key
and credentials
.
setApiKey
method inTranslateOptions
is @Deprecated. So how can I replace it with my own key from GCP Console? @DalmTo – TextAIzaSyANUEzYsK***
. But it does not work. – Text