Google Cloud Translation API The request is missing a valid API key
Asked Answered
P

4

6

I'm trying to use the Google Cloud Translation API in my application but whenever I try to translate something it comes up with this missing valid API error.

I've done the quickstart steps and that didn't work.

I've tried the steps in the client library authentication and that hasn't worked either.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: herrsa1.bit.translator, PID: 16598
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)
  .. 18 more
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

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"
  }

  at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
  ... 4 more
  at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:130)
  ... 19 more
Piave answered 28/5, 2018 at 13:32 Comment(1)
can you share your sample codeSarthe
S
6

If you are using client library and you have already downloaded your service account json file, try doing this:

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

instead of this:

// Instantiates a client
const translate = new Translate({projectId});

This way you need only your the service acount json file and the specific API enabled

Sarthe answered 6/7, 2019 at 9:32 Comment(0)
L
2

The error on API key means you didn't create or use the key properly. You need to do the following for the key to work:

  1. Create a service account
  2. Create a key for above service account
  3. Download the key to a location, for example, a local path
  4. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the key, refer to samples in Quickstart tutorial

I'd reccomend doing #1 & #2 in GCP Console, and handling #3 & #4 in Cloud Shell.

Longlongan answered 3/8, 2018 at 19:4 Comment(0)
S
0

I also tried to execute this sample program. I followed the same instruction. But when I executing I got same error(The request is missing a valid API key).

I changed a line in the sample program.

Instead of

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

I added

Translate translate = TranslateOptions
            .newBuilder()
            .setCredentials(
                ServiceAccountCredentials
                            .fromStream(new FileInputStream(
                                    "YourCredentialFilePath.json")))
            .build().getService();

Now it is working.

Sample code after fix.

// Imports the Google Cloud client library
import java.io.FileInputStream;
import com.google.auth.oauth2.ServiceAccountCredentials;
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
      //Removed next line
      //Translate translate = TranslateOptions.getDefaultInstance().getService();
      //Added this line
      Translate translate = TranslateOptions
            .newBuilder()
            .setCredentials(
            ServiceAccountCredentials
                            .fromStream(new FileInputStream(
                                    "YourCredentialFilePath.json")))
            .build().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());
   }
}   
Sprouse answered 18/7, 2019 at 12:47 Comment(0)
S
0

The key your using does not have the permission to use Translate APIs.

To fix this :

  1. Go to Google Cloud Platform console

  2. Chose your project from the drop down menu in the top bar

  3. Go to API & Services > Library

  4. Search for Cloud Translation API and click on it

  5. Enable it

    enable api button

  6. Go to API & Services > Credentials

  7. Select the key you are using in your Android App

  8. From the menu called Restrict key, choose Cloud Translation API

  9. Save your edit

Now the APIs will work properly.

Schlep answered 20/9, 2021 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.