title: How to force download of file from Google Team Drive using Google Drive API libraries in Java
body: <p>In my application, I am trying to force a download of a file from a Google Team Drive when the user clicks on a link on a form in my application.</p>
  
  <p>A few updates:</p>
  
  <ol>
  <li><p>I have code that can upload to the Team Drive as well as create    folders on the Team Drive.</p></li>
  <li><p>The "Team Drives support - This application works properly with files    in Team Drives." on the Google Drive API settings CANNOT be enabled -    I can check this box, but I cannot click the Save Changes button,    since the button is always disabled.  However, I don't think that is    the issue, since I can interact with the Team Drive in my code.</p></li>
  </ol>
  
  <p>I have read the Google documentation, which is very limited:</p>
  
  <p><a href="https://developers.google.com/drive/v3/web/manage-downloads" rel="nofollow noreferrer">https://developers.google.com/drive/v3/web/manage-downloads</a></p>
  
  <p><a href="https://developers.google.com/drive/v3/reference/files/get" rel="nofollow noreferrer">https://developers.google.com/drive/v3/reference/files/get</a></p>
  
  <p>I have also read through a few posts:</p>
  
  <p><a href="https://stackoverflow.com/questions/45182550/download-files-from-google-drive-using-java-api">Download files from google drive using java API</a></p>
  
  <p>But, I can't find an answer that works.  I have built some code which uses a Service Account.  The code sets the drive service and uses the Service Account as the credentials.  I based the download part of my code from this Google documentation example:</p>
  
  <p><a href="https://developers.google.com/drive/v3/web/manage-downloads#examples" rel="nofollow noreferrer">https://developers.google.com/drive/v3/web/manage-downloads#examples</a></p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>public static void downloadFile(String fileID) {
      // Set the drive service...
      Drive service = null;
      try {
          service = getDriveService();
      } catch (IOException e) {
          e.printStackTrace();
      }
      OutputStream outputStream = new ByteArrayOutputStream();
      try {
          service.files().get(fileID).executeMediaAndDownloadTo(outputStream);
          // NEED CODE HERE???????
  
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  
  /**
   * Build and return an authorized Drive client service.
   * 
   * @return an authorized Drive client service
   * @throws IOException
   */
  public static Drive getDriveService() throws IOException {
      Logger.info("GoogleDrive: getDriveService: Starting...");
      GoogleCredential credential = null;
      Drive googleDrive = null;
      try {
          credential = authorize();
          Logger.info("GoogleDrive: getDriveService: Credentials set...");
          googleDrive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                  .setApplicationName(APPLICATION_NAME).build();
      } catch (IOException ex) {
          System.out.println(ex.toString());
          ex.printStackTrace();
      }
      return googleDrive;
  }
  
  /**
   * Creates an authorized Credential object.
   * 
   * @return an authorized Credential object.
   * @throws IOException
   */
  @SuppressWarnings("deprecation")
  public static GoogleCredential authorize() throws IOException {
      GoogleCredential credential = null;
      String credentialsFileName = "";
      try {
          Logger.info("GoogleDrive: authorize: Starting...");
  
          Logger.info("GoogleDrive: authorize: Getting credentialsFileName path...");
          credentialsFileName = Configuration.root().getString("google.drive.credentials.file");
          Logger.info("GoogleDrive: authorize: credentialsFileName = " + credentialsFileName);
  
          Logger.info("GoogleDrive: authorize: Setting InputStream...");
          InputStream in = GoogleDrive.class.getClassLoader().getResourceAsStream(credentialsFileName);
          if (in == null) {
              Logger.info("GoogleDrive: authorize: InputStream is null");
          }
          Logger.info("GoogleDrive: authorize: InputStream set...");
  
          Logger.info("GoogleDrive: authorize: Setting credential...");
          credential = GoogleCredential.fromStream(in, HTTP_TRANSPORT, JSON_FACTORY)
                  .createScoped(Collections.singleton(DriveScopes.DRIVE));
      } catch (IOException ex) {
          System.out.println(ex.toString());
          System.out.println("Could not find file " + credentialsFileName);
          ex.printStackTrace();
      }
      Logger.info("GoogleDrive: authorize: Ending...");
      return credential;
  }
  </code></pre>
  
  <p>When my code runs, there are no errors and nothing happens.  I am guessing I am missing some code in the downloadFile function.  If I am missing something or incorrect in my coding, please feel free to provide examples or the correct code I should be using.</p>
  
  <p>I appreciate the help.</p>