Google cloud translate API in Java not working, Dont know how to use API keys as well

865 views
Skip to first unread message

Amit Kumar

unread,
Dec 21, 2016, 9:06:07 AM12/21/16
to Google Cloud Translation API

I am trying to use Google cloud translate API in Java. I have signed up free trial version and enable translate API project from google cloud console. Also I have API key with me.

I referred to API usage reference created java maven project included dependency of google-cloud-translate. Its downloaded also as a dependency. In java class file I am using same code as given the link but its giving me below error.

I have not used API keys anywhere as I don't know where to use it. I think that's the problem but don't know how to pass API keys for authentication? So I missed step "gcloud beta auth application-default login" in the link as running it on command line doesn't work.

I have java project in eclipse and I am using "Run As"-> "Java application" to run it.

    Exception in thread "main" com.google.cloud.translate.TranslateException: connect timed out
    at com.google.cloud.translate.spi.DefaultTranslateRpc.translate(DefaultTranslateRpc.java:63)
    at com.google.cloud.translate.spi.DefaultTranslateRpc.translate(DefaultTranslateRpc.java:145)
    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.cloud.RetryHelper.doRetry(RetryHelper.java:179)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
    at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:110)
    at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:124)
    at com.translate.main.Translator.main(Translator.java:83)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:656)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1104)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
    at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:93)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:972)
    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.DefaultTranslateRpc.translate(DefaultTranslateRpc.java:125)
    ... 7 more

Alex (Cloud Platform Support)

unread,
Dec 22, 2016, 6:04:31 PM12/22/16
to Google Cloud Translation API

Hi Amit,


Note that this type of question is more appropriate for Stack Overflow which we regularly monitor as other types of stack exchange services and which would increase your chances to get a faster answer due to its larger audience.


I was unable to reproduce your error using your referenced Cloud Translation API page, however you may be interested in testing the API locally using this Sample Application link which worked on my end.


As per the API key, it is recommended to be used when your application is running on a server and accessing one of the following kinds of data:


  • Data that the data owner has identified as public, such as a public calendar or blog.

  • Data that is owned by a Google service such as Google Maps or Google Translate. (Access limitations might apply.)


As your are running your application locally, the API key should not be the reason for your error. Here are the recommended Best Practices on how to use the API keys.

Amit Kumar

unread,
Dec 23, 2016, 4:54:55 AM12/23/16
to Google Cloud Translation API
Actually the error was coming due to me running the code behind corporate proxy. I just added below lines(a static initializer) to my java code and bingo !!!

 static
    {
        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
        System.setProperty("http.proxyPort", HTTP_PROXY_PORT);
        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
        System.setProperty("https.proxyPort", HTTPS_PROXY_PORT);
    }

where HTTP_PROXY_HOST and other are defined constants for my company...

Alex (Cloud Platform Support)

unread,
Dec 23, 2016, 11:23:02 AM12/23/16
to Google Cloud Translation API

Happy to hear that you made it work and thanks for sharing your solution. It is in fact one of the most common reasons for authentication failures and not always easy to identify.


Good job!  

Sergio Rodríguez Sansano

unread,
Jul 29, 2019, 10:31:02 AM7/29/19
to Google Cloud Translation API
Hi all,

The static code above would apply for the entire app. Is there any way of overriding HTTP settings for calls to Google Translate only? Any other call would use default proxy settings.

Regards

Sergio

Ali T (Cloud Platform Support)

unread,
Jul 30, 2019, 4:47:21 PM7/30/19
to Google Cloud Translation API
Hey,

I came across this tutorial which breaks down how to connect through a proxy on a per connection basis rather on a system wide property. This looks like an appropriate solution for your use case. 

Sergio Rodríguez Sansano

unread,
Jul 31, 2019, 2:33:37 PM7/31/19
to Google Cloud Translation API
Hi,

I've ended up writing my own TransportOptions with the proxy I want as in here:


HttpTransportFactory hf = () -> {
try {
return new NetHttpTransport.Builder().trustCertificates(GoogleUtils.getCertificateTrustStore())
.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()))).build();
} catch (GeneralSecurityException | IOException e) {
LOGGER.warn("Cannot create HTTP Transport to Google API with current proxy settings: {}:{}", proxyConfig.getHost(),
proxyConfig.getPort());
return new NetHttpTransport();
}
};
this.transportOptions = HttpTransportOptions.newBuilder().setHttpTransportFactory(hf).build();

Cheers

Sergio
Reply all
Reply to author
Forward
0 new messages