Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 824
Federer Avatar asked Mar 20 '18 08:03

Federer


People also ask

How do I install Google Translate API?

Get started with Google Translate API in memoQ:From the projects list, select a project or create a new one. If the APIs & services page isn't already open, open the console left side menu and select APIs & services, and then select Library. Search for the Translate API with the search function. Click ENABLE.

How do I use Google API for translation in python?

Translating Text Documents You can also translate text documents via Google Translate API. All you have to do is to read the text file in Python using the open method, read the text and pass it to the translate() method.


2 Answers

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

like image 138
Yusuf Khan Avatar answered Oct 13 '22 10:10

Yusuf Khan


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

like image 22
DaImTo Avatar answered Oct 13 '22 09:10

DaImTo