0
votes

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" }    
1
Are you getting this error while running locally or deploying? If the latter, what GCP product are you using? I am a bit lost with the error as it is saying 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
Did you follow steps like here: help.memsource.com/hc/en-us/articles/… - Brooklyn99
@Santos I followed steps similar to what were listed on the link that you provided and had no problems with them. I was able to register the account and generate the API key. My issue is I'm not sure where in the program or in the Google Cloud Platform libraries I have to enter it. - ZeroNicks
@KF I am running this program locally, using Eclipse on Windows 10. The Service account has been created and I can generate API keys. I've taken a look at the TranslateOption class but I'm not sure if I can enter in the APIKey as part fo the strong declaration or is it supposed to be part of the main Quickstart class? - ZeroNicks

1 Answers

0
votes

Reading the documentation this is the way to add the api_key for google cloud translation. I tested it and it work on both ways.

"Notice that this code can be also used with an API key. By default, an API key is looked for in the GOOGLE_API_KEY environment variable. Once the API key is set, you can make API calls by invoking methods on the Translation service created via TranslateOptions.getDefaultInstance().getService().

You can also explicitly set the API key as follows:"

Translate translate = TranslateOptions.newBuilder().setApiKey("myKey").build().getService();

https://cloud.google.com/docs/authentication/production

https://github.com/googleapis/java-translate