title: Google Drive API Java client behind proxy
body: <p>I am trying to write a java client for Drive API using <a href="https://developers.google.com/resources/api-libraries/documentation/drive/v2/java/latest/" rel="nofollow">Drive API v2</a>. However I am getting an error while trying to perform OAuth2 Authentication in my code. I think this might be a proxy issue, which I tried to resolve by setting proxy in my code, but it did not help.</p>
  
  <p><a href="https://developers.google.com/drive/v2/web/quickstart/java#prerequisites" rel="nofollow">This</a> is the guide which I referred to, for writing my program.</p>
  
  <p>Here's a snippet of my code:</p>
  
  <pre><code>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();
          }
  </code></pre>
  
  <p>Here's the stacktrace:</p>
  
  <pre><code>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&amp;redirect_uri=http://localhost:51307/Callback&amp;response_type=code&amp;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.&lt;init&gt;(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.&lt;init&gt;(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)
  </code></pre>
  
  <p>I opened the authentication link my browser and after approving the permissions it showed the following message : </p>
  
  <blockquote>
    <p>Received verification code. You may now close this window...</p>
  </blockquote>
  
  <p>Hope someone can point me in the right direction.</p>