title: How to use HttpClient libraries to download a file from Google Drive?
body: <p>I am attempting to integrate Google Picker on a page. The user uses the picker to sign in into his google account and picks a file. I am trying to use Apache HttpClient libraries to download this file.</p>
  
  <p>Here are the implementation details :</p>
  
  <p>I used Client Side Auth on Javascript as highlighted on this <a href="https://developers.google.com/picker/docs/" rel="nofollow">Google Tutorial</a>. Once I got the 'fileId' of the file, I performed an authenticated <a href="https://developers.google.com/drive/v2/reference/files/get" rel="nofollow">GET</a> using Google Drive API to retrieve the file metadata - name, user, dates, downloadUrl, exportLinks etc. You can use the above link to test the end point and see the responses if you have a fileId.</p>
  
     <pre class="lang-js prettyprint-override"><code>   function postDownload (resp) {
          var link;
          var data = $.parseJSON (resp);
          console.log (data.webContentLink);
          if (data.mimeType == "application/pdf"){
              link = data.downloadUrl;
          } else {
              link = (data.exportLinks)['application/pdf'];
          }
          //I wish to use the below link to download the file on server side
          //using HttpClient libraries.
          console.log ("Download Link - " +link);
      }
  
      //Call back after a file is selected on the picker
      function pickerCallback(data) {
          if (data.action == google.picker.Action.PICKED) {
              var id = data.docs[0].id;
              var accessToken = gapi.auth.getToken().access_token;
              console.log (id);
  
              var xhr = new XMLHttpRequest();
              xhr.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
              xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
              xhr.onload = function() {
                  postDownload(xhr.responseText);
              };
              xhr.onerror = function() {
                  checkAuth();
              };
              xhr.send();
          }
      }
  </code></pre>
  
  <p>Download Code:</p>
  
  <pre class="lang-java prettyprint-override"><code>    import java.io.IOException;
      import org.apache.http.Header;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.impl.client.DefaultHttpClient;
  
      public class DownloadFile {
  
      public static void main(String[] args) throws IOException {
          String link = "https://docs.google.com/uc? id=0ByJwoNOQMrO_S3hlWGRfa2JwVWM&amp;export=download";
  
          DefaultHttpClient httpclient = new DefaultHttpClient();
          HttpGet get = new HttpGet(link);
          HttpResponse response = httpclient.execute(get);
  
          Header[] headers = response.getAllHeaders();
          for (Header header : headers) {
              System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue());
          }
      }
  
  }
  </code></pre>
  
  <p>If I use the link on the browser or the POSTMAN Chrome Extension, I am able to download the file correctly. The REST clients reports the incoming data correctly as 'application/pdf'.</p>
  
  <p>However, when I use the above Snippet to perform a HttpGet on the URL, the incoming data has a content type of 'text/html' . No clue about this behavior. I don't think the final URL needs to be treated any differently to download the file.</p>
  
  <p>Has anybody faced this issue? Thanks in advance.</p>