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

扶醉桌前 提交于 2019-12-10 19:25:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/10934525/unable-to-retrieve-google-drive-files-and-folders-using-new-drive-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!