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

前端 未结 3 1657
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-21 07:29

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

相关标签:
3条回答
  • 2020-12-21 07:36

    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

    0 讨论(0)
  • 2020-12-21 07:45

    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

    0 讨论(0)
  • 2020-12-21 07:55

    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());
       }
    }   
    
    0 讨论(0)
提交回复
热议问题