【问题标题】:Google Cloud Translation API The request is missing a valid API keyGoogle Cloud Translation API 请求缺少有效的 API 密钥
【发布时间】:2018-11-07 02:56:48
【问题描述】:

我正在尝试在我的应用程序中使用 Google Cloud Translation API,但每当我尝试翻译某些内容时,它就会出现这个缺少有效 API 的错误。

我已经完成了quickstart steps,但没有成功。

我已经尝试了client library authentication 中的步骤,但也没有用。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: herrsa1.bit.translator, PID: 16598
com.google.cloud.translate.TranslateException: The request is missing a valid API key.

  at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
  .. 18 more
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

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.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
  ... 4 more
  at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:130)
  ... 19 more

【问题讨论】:

  • 你能分享你的示例代码吗

标签: android google-translate google-translation-api google-cloud-iam


【解决方案1】:

如果您正在使用客户端库并且您已经下载了您的服务帐户 json 文件,请尝试这样做:

// Instantiates a client
const translate = new Translate({
    projectId: 'your project id', //eg my-proj-0o0o0o0o'
    keyFilename: 'path of your service acount json file' //eg my-proj-0fwewexyz.json
}); 

而不是这个:

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

这样您只需要您的服务帐户 json 文件和启用的特定 API

【讨论】:

    【解决方案2】:

    API 密钥错误意味着您没有正确创建或使用密钥。您需要执行以下操作才能使密钥起作用:

    1. Create a service account
    2. Create a key for above service account
    3. 将密钥下载到某个位置,例如本地路径
    4. 设置环境变量GOOGLE_APPLICATION_CREDENTIALS为key的文件路径,参考快速入门教程中的示例

    我建议在 GCP Console 中执行 #1 和 #2,并在 Cloud Shell 中处理 #3 和 #4。

    【讨论】:

      【解决方案3】:

      您使用的密钥没有使用翻译API的权限。

      解决这个问题:

      1. 转到Google Cloud Platform console

      2. 从顶部栏中的下拉菜单中选择您的项目

      3. 转到API & Services > Library

      4. 搜索Cloud Translation API并点击

      5. 启用它

      6. 转到API & Services > Credentials

      7. 选择您在 Android 应用中使用的密钥

      8. 从名为Restrict key 的菜单中,选择Cloud Translation API

      9. 保存您的编辑

      现在 API 可以正常工作了。

      【讨论】:

        【解决方案4】:

        我也尝试执行this 示例程序。 我遵循same 指令。但是当我执行时,我遇到了同样的错误(请求缺少有效的 API 密钥)。

        我在示例程序中更改了一行。

        代替

        Translate translate = TranslateOptions.getDefaultInstance().getService();
        

        我加了

        Translate translate = TranslateOptions
                    .newBuilder()
                    .setCredentials(
                        ServiceAccountCredentials
                                    .fromStream(new FileInputStream(
                                            "YourCredentialFilePath.json")))
                    .build().getService();
        

        现在可以正常使用了。

        修复后的示例代码。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-05
          • 1970-01-01
          • 2018-08-29
          • 1970-01-01
          • 2018-12-25
          • 1970-01-01
          • 2021-10-23
          • 2019-02-11
          相关资源
          最近更新 更多