Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to retrieve Google Drive files and folders using new Drive API

I am trying to get a list of Files in a Folder from Google Drive from my Android app but have been unsuccessful so far. I'm using google-api-drive-v1-rev4-java-1.6.0-beta and google-api-client-1.9.0. I'm also building my code similar to calendar-android-sample and tasks-android-sample from the samples at http://code.google.com/p/google-api-java-client/wiki/Android.

I cant seem to find how to use files() to get a list of folders or even the id of the folder I want. The tasks-android-sample uses '@default' in the get() method to get a list of tasks. What would I use in the get method to get a list of folders first, search for my folder, get the id, then get a list of files in that folder?

AsyncLoadDocs.java: (Note: I'm using getFields() just to see if the Get object contains any metadata, which at this point doesn't.)

package com.mysite.myapp.docs;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.Drive.Files.Get;
import com.google.api.services.drive.model.File;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Asynchronously load the docs with a progress dialog.
 *
 * @author ms
 */
class AsyncLoadDocs extends AsyncTask<Void, Void, List<String>> {

private static final String TAG = "AsyncLoadDocs";
private final GDocsSync gDocsSync;
private final ProgressDialog dialog;
private final Drive entry = null;
private com.google.api.services.drive.Drive service;

  AsyncLoadDocs(GDocsSync gDocsSync) {
    this.gDocsSync = gDocsSync;
    service = gDocsSync.driveClient;
    dialog = new ProgressDialog(gDocsSync);
  }

  @Override
  protected void onPreExecute() {
    dialog.setMessage("Loading docs...");
    dialog.show();
  }

  @Override
  protected List<String> doInBackground(Void... arg0) {
    try {
      List<String> folderNames = new ArrayList<String>();

      Get get = service.files().get("@default").setProjection("FULL");
      String fields = get.getFields();
      Log.d(TAG, "Fields: " + fields);

      return folderNames;
    } catch (IOException e) {
        gDocsSync.handleGoogleException(e);
      return Collections.singletonList(e.getMessage());
    } finally {
        gDocsSync.onRequestCompleted();
    }
  }

  @Override
  protected void onPostExecute(List<String> result) {
    dialog.dismiss();
  }
}

Any help would be appreciated. Both Calendar and Tasks samples successfully retrieve data from Google using my API key, why doesn't this Drive code?

like image 534
phoeniix Avatar asked Jan 20 '26 23:01

phoeniix


1 Answers

The Drive API grants access only to two classes of files:

  • Files that a user has created with a given Drive app
  • Files that a user opens with a given Drive app

For security reasons, there's no method to list all files in a user Drive account:

https://developers.google.com/drive/apps_overview#granting_file-level_access

For more options in the Android environment, check out these other answers:

  • Android API for Google Drive?
  • Google Drive\Docs API for Android
like image 198
Claudio Cherubino Avatar answered Jan 23 '26 15:01

Claudio Cherubino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!