title: How to download file from Google Drive using Drive.API?
body: <p>I am using Google Drive.Api in order to get user's app data synced with users drive account.</p>
  
  <p>The user database is in sqlite database format. I have successfully uploaded the file in binary to the drive but failing to download the file from within the app.</p>
  
  <p>How I get the file URl :</p>
  
  <pre><code>  final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
      credential.setSelectedAccountName(accountName);
      service = new com.google.api.services.drive.Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
  
      Query query = new Query.Builder()
              .addFilter(Filters.eq(SearchableField.TITLE, "BackupFile"))
              .build();
  
  
      final DriveFolder folder = Drive.DriveApi.getRootFolder(mGoogleApiClient);
      folder.queryChildren(mGoogleApiClient, query).setResultCallback(new ResultCallback&lt;DriveApi.MetadataBufferResult&gt;() {
          @Override
          public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) {
              final MetadataBuffer metadataBuffer = metadataBufferResult.getMetadataBuffer();
              int iCount = metadataBuffer.getCount();
  
              if (iCount == 1) {
                  Log.i("Tag", "file was found");
                  final DriveId myFileId = metadataBuffer.get(0).getDriveId();
                  final DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, myFileId);
  
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          DriveResource.MetadataResult mdRslt = file.getMetadata(mGoogleApiClient).await();
                          if (mdRslt != null &amp;&amp; mdRslt.getStatus().isSuccess()) {
                              String link = mdRslt.getMetadata().getWebContentLink();
                              Log.d("TAG", "Link to File:" + link);
                               DownloadBackup(link);
                          }
                      }
                  }).start();
  
              }
              metadataBuffer.release();
          }
      });
  </code></pre>
  
  <p>Trying to download the File via:</p>
  
  <pre><code>public void DownloadBackup(String url){
          InputStream mInput=null;
          FileOutputStream mOutput=null;
          if(url != null &amp;&amp; url.length() &gt; 0 ){
          try {
                  GoogleAccountCredential crd = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
                  crd.setSelectedAccountName(accountName);
                  com.google.api.services.drive.Drive srv = new com.google.api.services.drive.Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), crd).build();
                  mInput = srv.getRequestFactory().buildGetRequest(new GenericUrl(url)).execute().getContent();
                  String outFileName = getApplicationContext().getDatabasePath(DatabaseHandler.DATABASE_NAME).getPath();
                  mOutput = new FileOutputStream(outFileName);
                  byte[] mBuffer = new byte[1024];
                  int mLength;
                  while ((mLength = mInput.read(mBuffer)) &gt; 0) {
                      mOutput.write(mBuffer, 0, mLength);
                  }
                  mOutput.flush();
                  Log.d("TAG", "Successfully Downloaded contents");
              } catch (IOException e) {
                  e.printStackTrace();
              }finally {
                  try {
                      //Close the streams
                      if(mOutput != null){
                          mOutput.close();
                      }
                      if(mInput != null){
                          mInput.close();
                      }
                  } catch (IOException e) {
                      Log.e("Tag", "failed to close databases");
                  }
              }
          } else {
              // The file doesn't have any content stored on Drive.
              // return null;
              Log.e("Tag", "No content on Drive");
          }
      }
  </code></pre>
  
  <p>Error Log:</p>
  
  <pre><code>W/System.err﹕ com.google.api.client.http.HttpResponseException: 401 Unauthorized
  W/System.err﹕ &lt;HTML&gt;
  W/System.err﹕ &lt;HEAD&gt;
  W/System.err﹕ &lt;TITLE&gt;Unauthorized&lt;/TITLE&gt;
  W/System.err﹕ &lt;/HEAD&gt;
  W/System.err﹕ &lt;BODY BGCOLOR="#FFFFFF" TEXT="#000000"&gt;
  W/System.err﹕ &lt;H1&gt;Unauthorized&lt;/H1&gt;
  W/System.err﹕ &lt;H2&gt;Error 401&lt;/H2&gt;
  W/System.err﹕ &lt;/BODY&gt;
  W/System.err﹕ &lt;/HTML&gt;
  W/System.err﹕ [ 06-12 11:01:05.447 26208:26236 W/System.err ]
          at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1061)
  W/System.err﹕ at com.example.app.activities.Main.DownloadBackup(Main.java:487)
  W/System.err﹕ at com.example.app.activities.Main$15$1.run(Main.java:332)
  W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
  </code></pre>