How to Configure APIKey in Java for Google Cloud Translation

676 views
Skip to first unread message

Chetan

unread,
Dec 18, 2017, 10:21:03 AM12/18/17
to Google Cloud Translation API
Hi
How can I configure Apikey in my Java class. I am using google-cloud-translate 1.12.0 in Maven Entry. Appreciate any help, Thanks in advance

Also, as a start I am using one of google GIT published code

/**
     * Detect the language of input text.
     *
     * @param sourceText
     *            source text to be detected for language
     * @param out
     *            print stream
     */
    public static void detectLanguage(String sourceText, PrintStream out) {
        Translate translate = createTranslateService();
        List<Detection> detections = translate.detect(ImmutableList.of(sourceText));
        System.out.println("Language(s) detected:");
        for (Detection detection : detections) {
            out.printf("\t%s\n", detection);
        }
    }

    /**
     * Create Google Translate API Service.
     *
     * @return Google Translate Service
     */
    public static Translate createTranslateService() {
        return TranslateOptions.newBuilder().build().getService();
    }

    public static void main(String[] args) {
        detectLanguage("Hello", System.out);
    }

Error it gives
{
  "error": {
    "code": 403,
    "message": "The request is missing a valid API key.",
    "errors": [
      {
        "message": "The request is missing a valid API key.",
        "domain": "global",
        "reason": "forbidden"
      }
    ],
    "status": "PERMISSION_DENIED"
  }
}

Kenworth (Google Cloud Platform)

unread,
Dec 18, 2017, 3:55:43 PM12/18/17
to Google Cloud Translation API
For security reasons, the use of service account is preferred over API keys. You can follow this guide for service accounts or this GitHub example for API keys.
Reply all
Reply to author
Forward
0 new messages