0
votes

I have gone thru the Google cloud transalte API and decided to use Java platform for my application

Based on the documents, tested the below code with required jar's. this code working fine in my eclipse

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

        String name = rs.getString("name");
        Translation translation1 = translate.translate(name);
        String TransalateName = translation1.getTranslatedText();

However when export this code into a jar and running in Shell script/Unix server I'm getting the below error says, permission deined - The request is missing a valid API key. How and where can i use the API key in the java code, i have a authorized service account.

I have tried Google transalte with my service account it's working fine, below is the example. However I'm not sure how to use this key in JAVA code

CURL command - working fine

curl -X POST \
     -H "Authorization: Bearer "AUTH_TOKEN_HERE \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'q': '你好',
  'target': 'en',
  'model': 'nmt',
}" "https://translation.googleapis.com/language/translate/v2"

Error:

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)
        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 orderFullfilment.EventDriver.main(EventDriver.java:138)
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: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:1065)
        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)
        ... 8 more
1

1 Answers

0
votes

You're missing a call to GoogleAPI.setKey.

import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

GoogleAPI.setKey(API_KEY);
GoogleAPI.setHttpReferrer("https://...");
// normally you would catch errors
Translate.DEFAULT.execute(sourceText, Language.fromString(sourceLanguageCode),
            Language.fromString(targetLanguageCode));

See for example https://www.programcreek.com/java-api-examples/?api=com.google.api.GoogleAPI. I must say it's disappointing how there is no example (that I could find) in the google cloud docs.