无法使用新的Drive API检索Google云端硬盘文件和文件夹 [英] Unable to retrieve Google Drive files and folders using new Drive API

查看:228
本文介绍了无法使用新的Drive API检索Google云端硬盘文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的Android应用获取Google云端硬盘中文件夹中的文件列表,但目前为止尚未成功。我正在使用google-api-drive-v1-rev4-java-1.6.0-beta和google-api-client-1.9.0。我还在 http://code.google.com/p/google-api-java-client/wiki/Android



<我似乎无法找到如何使用文件()来获取文件夹的列表,甚至我想要的文件夹的ID。 tasks-android-sample使用get()方法中的@default来获取任务列表。我将在get方法中使用什么来获取文件夹列表,搜索我的文件夹,获取id,然后获取该文件夹中的文件列表?



AsyncLoadDocs.java:(注意:我使用getFields()来查看Get对象是否包含任何元数据,在这一点上不存在。)

  package com.mysite.myapp.docs; 

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

导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.ArrayAdapter;

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

/ **
*通过进度对话框异步加载文档。
*
* @author ms
* /
class AsyncLoadDocs extends AsyncTask< Void,Void,List< String>> {

private static final String TAG =AsyncLoadDocs;
private final GDocsSync gDocsSync;
私人最终ProgressDialog对话框;
private final Drive entry = null;
私人com.google.api.services.drive.Drive服务;

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

@Override
保护无效onPreExecute(){
dialog.setMessage(正在加载文档...);
dialog.show();
}

@覆盖
保护列表< String> doInBackground(Void ... arg0){
try {
List< String> folderNames = new ArrayList< String>();

get get = service.files()。get(@ default)。setProjection(FULL);
字符串字段= get.getFields();
Log.d(TAG,Fields:+ fields);

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


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

任何帮助将不胜感激。日历和任务示例都使用我的API密钥成功地从Google中检索数据,为什么不使用此驱动器代码?解析方案

驱动器API只允许访问两类文件:


  • 用户使用给定Drive应用程序创建的文件
  • 用户使用给定的Drive应用程序打开的文件



出于安全原因,没有办法列出所有文件用户云端硬盘帐户:



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



有关Android环境中的更多选项,请查看以下其他答案:




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?

解决方案

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:

这篇关于无法使用新的Drive API检索Google云端硬盘文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆