Google Translate Api无法访问可变环境Ubuntu

时间:2019-07-14 14:47:54

标签: java ubuntu netbeans google-translation-api

我无法配置Google Cloud API(Google翻译)。

我在视频中的所有操作:https://www.youtube.com/watch?v=8jjYTkSWD8s

按照说明进行所有操作-https://cloud.google.com/translate/docs/quickstart-client-libraries

1。创建一个项目
2.创建Api密钥并下载Json文件。
3.在〜/ .bashrc
中创建变量-GOOGLE_APPLICATION_CREDENTIALS 3.1执行命令 source〜/ .bashrc
4.将依赖项添加到Maven文件

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

5。导入的库

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

但是,当我尝试执行代码时-我得到了异常-线程“ main”中的异常com.google.cloud.translate.TranslateException:该请求缺少有效的API密钥。我该怎么办?

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

0 个答案:

没有答案