Search code examples
apigcloudtranslate

google cloud translate Java lib hang-up


  1. I have config the gcloud follow this link https://cloud.google.com/sdk/docs/quickstart-windows
  2. I have got the Credentialed Accounts image

  3. When I call the translate API or Speech API it's hang-up long time with the error message

    Exception in thread "main" com.google.cloud.translate.TranslateException: Error getting access token for service account: 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) at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89) at com.google.cloud.RetryHelper.run(RetryHelper.java:74) at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51) at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:110) at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:124) at org.org.fp.audio.recognition.core.TranslateTest.main(TranslateTest.java:16)

  4. The code will like this:

    public static void main(String... args) throws Exception {
    // 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());
    

    }

The issue is about account info or network ?


Solution

  • This is very likely an authorization issue:

    When using Google Cloud Client libraries, you need to download the Service Account's JSON file and set the GOOGLE_APPLICATION_CREDENTIALS to the path were your JSON file is stored. You may refer to this link for Translation API. The process is the same for Speech API.