Why is Google Cloud API trying to connect as an end-user?

萝らか妹 提交于 2019-12-07 01:40:21

问题


I am trying to use the Google Cloud Translate API. I generated a JSON file from a service account and set the GOOGLE_APPLICATION_CREDENTIALS to where the JSON file is saved. I then used it in a program like so:

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

But I get the following error

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

Here is my JSON file:

{
  "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"
}

Can anyone explain what is going on here? How can I fix it? Google Cloud SDK is not even installed.


回答1:


You are authenticated as a user, while the API expects a service account, seen that you use Client Libraries with the "import com.google.cloud.translate.*;" statement. You may find related detail in the Setting up authentication paragraph on the "Translation API Client Libraries" page.



来源:https://stackoverflow.com/questions/53000454/why-is-google-cloud-api-trying-to-connect-as-an-end-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!