Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代理背后的Google驱动API Java客户端_Java_Google Drive Api - Fatal编程技术网

代理背后的Google驱动API Java客户端

代理背后的Google驱动API Java客户端,java,google-drive-api,Java,Google Drive Api,我正在尝试使用为驱动器API编写java客户机。但是,我在代码中尝试执行OAuth2身份验证时出错。我认为这可能是一个代理问题,我试图通过在代码中设置代理来解决这个问题,但没有帮助 是我写程序时参考的指南 以下是我的代码片段: public static Credential authorize() throws IOException { // Load client secrets. Credential credential = null;

我正在尝试使用为驱动器API编写java客户机。但是,我在代码中尝试执行OAuth2身份验证时出错。我认为这可能是一个代理问题,我试图通过在代码中设置代理来解决这个问题,但没有帮助

是我写程序时参考的指南

以下是我的代码片段:

public static Credential authorize() throws IOException {
            // Load client secrets.
            Credential credential = null;

            try{

            GoogleClientSecrets clientSecrets =
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(new FileInputStream("client_secret.json")));

            // Build flow and trigger user authorization request.

            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                      HTTP_TRANSPORT, JSON_FACTORY, clientSecrets,
                      Collections.singleton(DriveScopes.DRIVE)).setDataStoreFactory(
                      DATA_STORE_FACTORY).build();

            credential = new AuthorizationCodeInstalledApp(
                flow, new LocalServerReceiver()).authorize("user");

            System.out.println(
                    "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
            }catch(Exception e){
                e.printStackTrace(); System.exit(0);
                }
            return credential;

        }

        /**
         * Build and return an authorized Drive client service.
         * @return an authorized Drive client service
         * @throws IOException
         */
        public static Drive getDriveService() throws IOException {
            System.setProperty("http.proxyHost","10.3.100.207") ;
            System.setProperty("http.proxyPort", "8080") ;

            Credential credential = authorize();
            return new Drive.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME)
                    .build();
        }
这是stacktrace:

2016-05-19 18:41:54.989:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2016-05-19 18:41:54.990:INFO::jetty-6.1.26
2016-05-19 18:41:55.001:INFO::Started SocketConnector@localhost:51307
Please open the following address in your browser:
  https://accounts.google.com/o/oauth2/auth?client_id=904439877486-uqiqmstss1bdcmhabt0gom905jo3hqsv.apps.googleusercontent.com&redirect_uri=http://localhost:51307/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive
2016-05-19 18:42:28.496:INFO::Stopped SocketConnector@localhost:51307
java.net.UnknownHostException: accounts.google.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
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:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258)
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:981)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:82)
at DriveAPITest.authorize(DriveAPITest.java:84)
at DriveAPITest.getDriveService(DriveAPITest.java:104)
at DriveAPITest.main(DriveAPITest.java:116)
Exception in thread "main" java.net.UnknownHostException: www.googleapis.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
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:264)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
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:981)
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 DriveAPITest.main(DriveAPITest.java:121)
2016-05-19 18:41:54.989:INFO::通过org.mortbay.log.StdErrLog登录到STDERR
2016-05-19 18:41:54.990:信息::jetty-6.1.26
2016-05-19 18:41:55.001:信息::开始SocketConnector@localhost:51307
请在浏览器中打开以下地址:
https://accounts.google.com/o/oauth2/auth?client_id=904439877486-uqiqmsts1bdcmhabt0gom905jo3hqsv.apps.googleusercontent.com和重定向=http://localhost:51307/Callback&response_type=code&scope=https://www.googleapis.com/auth/drive
2016-05-19 18:42:28.496:信息::已停止SocketConnector@localhost:51307
java.net.UnknownHostException:accounts.google.com
位于java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
位于java.net.socksocketimpl.connect(socksocketimpl.java:392)
位于java.net.Socket.connect(Socket.java:589)
位于sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
位于sun.net.NetworkClient.doConnect(NetworkClient.java:175)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
在sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:264)
位于sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1283)
位于sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1258)
位于sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
位于com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
位于com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
位于com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
在com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:158)
在com.google.api.client.GoogleAppis.auth.oauth2.GoogleAuthorizationCodeTokenRequest.execute(GoogleAuthorizationCodeTokenRequest.java:79)
位于com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorization(AuthorizationCodeInstalledApp.java:82)
在DriveAPITest.authorize(DriveAPITest.java:84)
在DriveApiest.getDriveService(driveApiest.java:104)上
位于DriveAPITest.main(DriveAPITest.java:116)
线程“main”java.net.UnknownHostException中的异常:www.googleapis.com
位于java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
位于java.net.socksocketimpl.connect(socksocketimpl.java:392)
位于java.net.Socket.connect(Socket.java:589)
位于sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
位于sun.net.NetworkClient.doConnect(NetworkClient.java:175)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
位于sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
在sun.net.www.protocol.https.HttpsClient.(HttpsClient.java:264)
位于sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:367)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105)
位于sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999)
位于sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
位于sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
位于com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:93)
位于com.google.api.client.http.HttpRequest.execute(HttpRequest.java:981)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
位于com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
位于DriveAPITest.main(DriveAPITest.java:121)
我在浏览器中打开了身份验证链接,在批准权限后,它显示以下消息:


收到验证码。您现在可以关闭此窗口


希望有人能给我指出正确的方向。

您应该定义代理和HttpTransport:

final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("your address", port));
final HttpTransport httpTransport = new NetHttpTransport.Builder().setProxy(proxy).build();

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
                                        .setDataStoreFactory(DATA_STORE_FACTORY)
                                        .setAccessType("offline")
                                        .build();
从一开始,我就可以访问公司代理后面的gsheet,只是用这种方式设置https代理

System.setProperty("https.proxyHost", "myipproxyaddress");
System.setProperty("https.proxyPort", "myipproxyport");

HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
以及中所述的剩余代码
如果我只设置http(非https)代理“已收到验证码。您现在可以关闭此窗口”,则无法正常工作。这意味着您正在做正确的事情,您走在正确的轨道上。你能详细说明确切的问题是什么吗?请包括错误日志。您当前的日志似乎没有问题。@我已经编辑了我的问题以包含完整的错误日志。请查收。打印此消息后,我的程序退出。就连我都不明白到底是什么问题。你现在可以试试直接连接而不是代理连接来确认这确实是根本原因吗?你能解决这个问题吗?@noogui不,我最终放弃了这个库,直接打电话给我