Google Cloud Translation API The request is missing a valid API key

烈酒焚心 提交于 2019-11-29 16:59:19

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 [1]
2) create a key for above service account [2]
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

Doing 1) and 2) in GCP Console, 3) and 4) in Cloud Shell would be the easiest.

[1] https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating_a_service_account
[2] https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys

If you are using client library and you have already downloaded your service account json file try below

 // 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
        }
    ); 

instead of

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

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

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());
   }
}   
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!