Google Drive API网址已更改

时间:2016-05-03 18:42:05

标签: java google-drive-api

应用程序正在生产服务器上运行,该服务器通过代理服务器访问https://www.googleapi.com。那么如何设置网址而不是https://www.googleapi.comhttp://www.googleapi.com:443

目前正在使用的示例代码是

    File dataStoreDir = new File(resourceDir, credentials);

    JsonFactory jacksonFactory = JacksonFactory.getDefaultInstance();

    //List<String> scopes = Arrays.asList(DriveScopes.DRIVE);
    List<String> scopes = Arrays.asList("http://www.googleapis.com/auth/drive:443");

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

    FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(dataStoreDir);


    String clientSecret = config.getClientSecret();

    InputStream in = new FileInputStream(new File(resourceDir, clientSecret));
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jacksonFactory, new InputStreamReader(in));


    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    httpTransport, jacksonFactory, clientSecrets, scopes)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline")
                    .setAuthorizationServerEncodedUrl("http://www.googleapis.com/auth/drive:443")
                    .build();
    Credential credential = new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize("user");

    logger.info("Credentials saved to " + dataStoreDir.getAbsolutePath());


    drive =  new Drive.Builder(
            httpTransport, jacksonFactory, credential)
            .setApplicationName(applicationName)
            .build();

如何更改上面代码中的网址? 当我更改drive实例创建

drive =  new Drive.Builder(
                httpTransport, jacksonFactory, credential)
                .setApplicationName(applicationName).setRootUrl("http://www.googleapis.com:443/")
                .build();

然后它给出了错误

    ERROR [2016-05-03 18:39:10,847] com.test.intranet.sao.GoogleDocSAO: Connection reset
! java.net.SocketException: Connection reset
! at java.net.SocketInputStream.read(SocketInputStream.java:209) ~[na:1.8.0_65]
! at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_65]
! at java.io.BufferedInputStream.fill(BufferedInputStream.java:246) ~[na:1.8.0_65]
! at java.io.BufferedInputStream.read1(BufferedInputStream.java:286) ~[na:1.8.0_65]
! at java.io.BufferedInputStream.read(BufferedInputStream.java:345) ~[na:1.8.0_65]
! at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:704) ~[na:1.8.0_65]
! at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:647) ~[na:1.8.0_65]
! at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:675) ~[na:1.8.0_65]
! at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1536) ~[na:1.8.0_65]
! at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) ~[na:1.8.0_65]
! at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) ~[na:1.8.0_65]
! at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:37) ~[google-http-client-1.20.0.jar:1.20.0]
! at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:94) ~[google-http-client-1.20.0.jar:1.20.0]
! at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:972) ~[google-http-client-1.20.0.jar:1.20.0]
! at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) ~[google-api-client-1.20.0.jar:1.20.0]
! at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) ~[google-api-client-1.20.0.jar:1.20.0]
! at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) ~[google-api-client-1.20.0.jar:1.20.0]

1 个答案:

答案 0 :(得分:0)

通过将所需的连接应用于Google Drive firewall and proxy setting中列出的以下主机和端口,Google驱动器可以通过代理服务器工作。

但请注意,Google目前不支持与其地址连接的身份验证。您必须绕过Google Apps管理员帮助页面中给定地址的身份验证,以允许Google使用您的代理。

How to configure your proxy server to work with Google Drive中提供了可能的代理服务器配置。