gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:21 26 4
gpt4 key购买 nike

我正在尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com