java - 为什么 Google Cloud API 会尝试以最终用户身份进行连接?

标签 java json authentication google-cloud-platform google-translate

我正在尝试使用 Google Cloud Translate API。我从服务帐户生成了一个 JSON 文件,并将 GOOGLE_APPLICATION_CREDENTIALS 设置为保存 JSON 文件的位置。然后我在这样的程序中使用它:

import com.google.cloud.translate.*;
...
Translate translate = TranslateOptions.getDefaultInstance().getService();
Translation translation = translate.translate(message);

但是我得到以下错误

com.google.cloud.translate.TranslateException: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/
        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 app.websockets.Messenger.onMessage(Messenger.java:31)
        at org.java_websocket.server.WebSocketServer.onWebsocketMessage(WebSocketServer.java:569)
        at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:709)
        at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:367)
        at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:212)
        at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:925)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.",
    "reason" : "rateLimitExceeded"
  } ],
  "message" : "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.",
  "status" : "PERMISSION_DENIED"
}
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
        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:321)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:130)
        ... 13 more

这是我的 JSON 文件:

{
  "type": "service_account",
  "project_id": "ecstatic-motif-220300",
  "private_key_id": "keyid",
  "private_key": "somekey",
  "client_email": "kokosole@ecstatic-motif-220300.iam.gserviceaccount.com",
  "client_id": "100208235593900994013",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/kokosole%40ecstatic-motif-220300.iam.gserviceaccount.com"
}

谁能解释一下这是怎么回事?我该如何解决?甚至还没有安装 Google Cloud SDK。

最佳答案

您已通过用户身份验证,而 API 需要一个服务帐户,因为您使用带有“import com.google.cloud.translate.*;”的客户端库陈述。您可以在 Setting up authentication 中找到相关详细信息“翻译 API 客户端库”页面上的段落。

关于java - 为什么 Google Cloud API 会尝试以最终用户身份进行连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53000454/

相关文章:

java - JavaFX中关闭子窗口后如何刷新父窗口?

C# 序列化字典 <ulong,ulong> 到 JSON

php - 检查用户是否登录的替代方法

svn - 使用 Google Code 和 TortoiseSVN 时出现授权错误

java - 将二叉树转换为 List<List<Node>>

java - 是否可以避免将对象声明为final?

java - 带有单个字母的正则表达式无法识别

javascript - AngularJS - 解析 JSON

java - 如何让Spring Boot的GET返回JSON

php - 无法在 Codeigniter 中使用社区身份验证登录