2

使用服务帐户时出现 berlow 错误。相同的代码在本地运行良好,并且能够使用服务帐户创建凭据。

....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)
4

1 回答 1

3

尝试以下...

  1. 在 linux 框中设置代理。
  2. 在创建存储时 - new NetHttpTransport() 提供您自己的方式来创建套接字并在传输中添加代理。

示例代码...

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());
        }

使用 .setTransport(myNetHttpTransport()) 而不是 NetHttpTransport() 。

我们在这方面花了很长时间。但截至目前,这似乎正在奏效。请让我知道是否有任何帮助...

于 2014-11-05T10:00:22.927 回答