Missing a valid API key about Google Translation API Client issue?
T

5

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

Text answered 20/3, 2018 at 8:57 Comment(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.Triumphant
setApiKey method in TranslateOptions is @Deprecated. So how can I replace it with my own key from GCP Console? @DalmToText
Assuming that you have found an API key try setting it in GOOGLE_API_KEY Your looking for a public API key.Triumphant
I have found API key in Console, but I don't know where to set GOOGLE_API_KEY.Text
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 keyTriumphant
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.Text
T
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

Triumphant answered 20/3, 2018 at 10:17 Comment(1)
I have post a picture in my question. I am not sure is that right? Or I need set it in Path variable.Text
A
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

Apeman answered 6/7, 2019 at 10:0 Comment(1)
Translate is an interface...not a class dudeHolism
T
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

Triumphant answered 20/3, 2018 at 10:17 Comment(1)
I have post a picture in my question. I am not sure is that right? Or I need set it in Path variable.Text
I
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.

Irrupt answered 2/4, 2018 at 16:5 Comment(1)
@Irrupt 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 cloud.google.com/docs/authentication.",Yonah
W
2

Add

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

before

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

Cheers :)

Wendellwendi answered 14/11, 2019 at 8:52 Comment(1)
Why is it required to set the key sith System.setProperty()? Can't the sdk get the key from google-services.json file where the key already exists?Scammony
R
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)
    }
Rebecarebecca answered 23/11, 2020 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.