0

I can not configure Google Cloud API (google translate).

All my steps in video:https://www.youtube.com/watch?v=8jjYTkSWD8s

Did everything according to the instructions - https://cloud.google.com/translate/docs/quickstart-client-libraries

1.Created a project
2.Created Api key and downloaded Json file.
3.Created variable - GOOGLE_APPLICATION_CREDENTIALS in ~/.bashrc
3.1 Performed command source ~/.bashrc
4.Added dependencies to Maven file

<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-translate</artifactId>
  <version>1.82.0</version>
</dependency>

5.Imported libraries

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

But, when i try to perform my code - i get exception - Exception in thread "main" com.google.cloud.translate.TranslateException: The request is missing a valid API key. What do i do wrong?

--- exec-maven-plugin:1.2.1:exec (default-cli) @ MyProject ---
Jul 14, 2019 8:20:29 PM com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.
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:62)
    at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:156)
    at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:124)
    at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:121)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
    at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:120)
    at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:138)
    at com.mycompany.myproject.QuickstartSample.main(QuickstartSample.java:25)
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.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:451)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1089)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:549)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:482)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:599)
    at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:142)
    ... 8 more
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 2.021s
Finished at: Sun Jul 14 20:20:30 MSK 2019
Final Memory: 9M/162M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project MyProject: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Hideyoshi
  • 1
  • 1
  • Already added. Thanks. – Hideyoshi Jul 14 '19 at 17:28
  • Where are you specifying your key? See [this answer](https://stackoverflow.com/a/49559395/2985643) to [Google Cloud Translate - 403 Forbidden Missing API key](https://stackoverflow.com/q/49509391/2985643) for sample code, and a link to other examples. It looks like you need to add calls to `GoogleAPI.setKey()` and `GoogleAPI.setHttpReferrer()`. – skomisa Jul 14 '19 at 18:09
  • GoogleAPI class doesn't avaliable in code...(((Environment variable specifyed in ubuntu file~/.bashrc. I also tryed specify this variable use comand "export". This method doesn't work too.(( First time i cant't find good instruction using google services. – Hideyoshi Jul 14 '19 at 19:07

0 Answers0