I'm trying to learn how the Google Translation API works with Java, but I'm having trouble figuring out how to enter in the API key for Authorization. The API Key instructions say to pass it through using the key=API_KEY parameter, but I'm not sure where this is entered. Is this added to the main Class or entered somewhere in another sub class?
I'm using Eclipse running on Windows 10 as my platform. My basic configuration is from https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-java. Following these steps, I've registered with Google and created the API-Key, downloaded the .JSON file to my laptop, and installed the Google Cloud Tools for Eclipse and followed these instructions for configuring at this URL https://cloud.google.com/eclipse/docs/libraries?utm_source=github&utm_medium=google-cloud-java&utm_campaign=ToolsforEclipse.
The code that I'm using to test with is the example Java code found in the Quickstart at https://cloud.google.com/translate/docs/quickstart-client-libraries#client-libraries-install-java.
// Imports the Google Cloud client library
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class QuickstartSample {
public static void main(String... args) throws Exception {
// 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've tried modifying my Windows Environment variables to point the PATH variable GOOGLE_APPLICATION_CREDENTIALS=c:_project\translation.json but that did not seem to make any difference.
This is the error message that Eclipse returns when I try to run the program,
Aug. 25, 2019 10:08:29 P.M.
com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.
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:62)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:156)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:124)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:121)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:120)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:138)
at com.example.translate.QuickstartSample.main(QuickstartSample.java:35)
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" }
missing a valid API key. Did you created the service account as described here? Anyways, you might want to become familiar with the docs: googleapis.dev/java/google-cloud-clients/latest/com/google/… specially the "TranslateOption" class with provide mechanisms to explicitly defined your own credentials key. - K F