3

I am getting the berlow error while using the service account. The same code works well in local and iam able to create the credentials using service account.

....Error Snapshot : 
aused by: java.net.SocketTimeoutException: connect timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
        at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:270)
        at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:327)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
        at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
        at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1090)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
        at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
        at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
        at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
        at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)
2
  • Code Snapshot ------>> GoogleCredential credential = new GoogleCredential.Builder() .setTransport(TRANSPORT) .setJsonFactory(jsonFactory) .setServiceAccountId("SERVICE_ACCOUNT_EMAIL") .setServiceAccountPrivateKeyFromP12File( new java.io.File("SERVICE_ACCOUNT_PKCS12_FILE_PATH")) .setServiceAccountScopes(scopes) .setServiceAccountUser("[email protected]").build(); Commented Nov 5, 2014 at 5:04
  • I have got the answer but somehow not able to post it... I will post it once i m given the access Commented Nov 5, 2014 at 9:06

1 Answer 1

3

Try the following ...

  1. Set the proxy in the linux box.
  2. While creating storage - new NetHttpTransport() provide your own way to create the socket and add the proxy in the transport.

Example code ...

public HttpClient myHttpClient() throws Exception {

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        //SetRegisrty for both HTTP and HTTPS - Check google for this.
        schemeRegistry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), YOUR_PROXY_PORT));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory
                .getSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, 30 * 1000);  // SET the timeout
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        ClientConnectionManager connManager = new ThreadSafeClientConnManager(
                params, schemeRegistry);
        DefaultHttpClient httpClient = new DefaultHttpClient(connManager,
                params);
        try {
            int proxyPort = YOUR_PROXY_PORT;
            String proxyHost = "YOUR_PROXT_HOST_NAME";
            if (proxyPort > 0 && proxyHost != null && proxyHost.length() > 0) {
                System.setProperty("https.proxyHost", proxyHost);
                System.setProperty("https.proxyPort", proxyPort + "");
                System.setProperty("http.proxyHost", proxyHost);
                System.setProperty("http.proxyPort", proxyPort + "");
                HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                httpClient.getParams().setParameter(
                        ConnRoutePNames.DEFAULT_PROXY, proxy);
            }
        } catch (NullPointerException e) {
            System.out.println("Proxy error here");
        }
        return httpClient;
    }


    public static HttpTransport myNetHttpTransport()
                throws Exception {
            return new ApacheHttpTransport(myHttpClient());
        }

use .setTransport(myNetHttpTransport()) instead of NetHttpTransport() .

we spent long time on this. but as of now this seems to be working. Please let me know if any help in this...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.