7

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.

enter image description here

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
Federer
  • 73
  • 1
  • 1
  • 6
  • Please edit your question and include your code. API key is for use with accessing public apis and is not the same as application credentials. – Linda Lawton - DaImTo Mar 20 '18 at 09:27
  • `setApiKey` method in `TranslateOptions` is @Deprecated. So how can I replace it with my own key from GCP Console? @DalmTo – Federer Mar 20 '18 at 10:00
  • Assuming that you have found an API key try setting it in GOOGLE_API_KEY Your looking for a public API key. – Linda Lawton - DaImTo Mar 20 '18 at 10:13
  • I have found API key in Console, but I don't know where to set GOOGLE_API_KEY. – Federer Mar 20 '18 at 10:25
  • its an environment variable just like GOOGLE_APPLICATION_CREDENTIALS points to the location of the service account json file. GOOGLE_API_KEY you set to the value of the key – Linda Lawton - DaImTo Mar 20 '18 at 10:26
  • I have already create a new system variable GOOGLE_API_KEY and it's value is a string like `AIzaSyANUEzYsK***`. But it does not work. – Federer Mar 20 '18 at 10:29

5 Answers5

6

you can try to authenticating by your service acount json file like below

its pretty simple in node

 // Instantiates a client
 const translate = new Translate(
        {
                projectId: 'your project id', //eg my-project-0o0o0o0o'
                keyFilename: 'path of your service acount json file' //eg my-project-0fwewexyz.json
        }
    ); 

you can take the reference https://cloud.google.com/bigquery/docs/authentication/service-account-file for java

Yusuf Khan
  • 3,032
  • 3
  • 24
  • 31
4

To make authenticated requests to Google Translation, you must create a service object with credentials or use an API key. The simplest way to authenticate is to use Application Default Credentials. These credentials are automatically inferred from your environment, so you only need the following code to create your service object:

Translate translate = TranslateOptions.getDefaultInstance().getService();

I have personally never gotten that to work.

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().

Sample project here

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • I have post a picture in my question. I am not sure is that right? Or I need set it in `Path` variable. – Federer Mar 20 '18 at 11:27
2

I was able to get google translate to work by running "gcloud auth application-default login" on the command prompt. This regenerated the credentials to the default location after asking you to authorize with your google account. See https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-translate for more details.

user1694733
  • 121
  • 2
  • 4
  • @user1694733 however I recieved the following: "message" : "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.", – Alex Mi Jun 29 '19 at 15:07
2

Add

System.setProperty("GOOGLE_API_KEY", "your key here");

before

Translate translate = TranslateOptions.getDefaultInstance().getService();

Cheers :)

0
    {
      "code" : 403,
      "errors" : [ {
        "domain" : "global",
        "message" : "Requests from this Android client application <empty> are blocked.",
        "reason" : "forbidden"
      } ],
      "message" : "Requests from this Android client application <empty> are blocked.",
      "status" : "PERMISSION_DENIED"
    }

Following Approach I follow in android to resolve it. I tried with Private key but did not work for me. so I use public key for this

System.setProperty("GOOGLE_API_KEY", "Public key"); val translate = TranslateOptions.getDefaultInstance().service

    translate?.let {
        val translation =  it.translate("Hola Mundo!",
            Translate.TranslateOption.sourceLanguage("es"),
            Translate.TranslateOption.targetLanguage("en"),
            Translate.TranslateOption.model("nmt"));
     val check =  translation.translatedText
        Log.e("inf",""+check)
    }
Elletlar
  • 3,136
  • 7
  • 32
  • 38