2013-03-26 220 views
3

我正在创建一个可以将文件插入Google云端硬盘并将其列出的应用程序。我正在使用Google Drive SDK v2 API。但我的问题是它没有列出没有通过我的应用程序上传的文件。如果我直接从Google驱动器上传,它不会列在我的应用程序中。Google云端硬盘SDK API未列出Google云端硬盘上传的文件

这里列出文件的方法:

private static List<File> retrieveAllFiles(Drive service) throws IOException { 
     List<File> result = new ArrayList<File>(); 
     Files.List request = service.files().list(); 

     do { 
      try { 
      FileList files = request.execute(); 

      result.addAll(files.getItems()); 
      request.setPageToken(files.getNextPageToken()); 
      } catch (IOException e) { 
      System.out.println("An error occurred: " + e); 
      request.setPageToken(null); 
      } 
     } while (request.getPageToken() != null && 
       request.getPageToken().length() > 0); 

     return result; 
     } 

,我遍历文件是这样的:

List<File> files = retrieveAllFiles(service); 
     for(File f : files) { 
      System.out.println("File Name : "+f.getOriginalFilename(); 
     } 

谁能帮助我吗?在此先感谢...

+0

我有同样的问题。你能告诉我如何解决它?我与它连接数天 – Dolphin 2013-07-19 09:03:36

+0

更改oauth范围到'https:// www.googleapis.com/auth/drive' – 2013-07-19 09:17:00

+0

我正在使用java android。我不知道在哪里改变这个范围。这里是我的代码,你可以看看http://pastebin.com/pTFaVs4A – Dolphin 2013-07-19 09:29:19

回答

2

我认为你使用错误的oauth范围,可能https://www.googleapis.com/auth/drive.file限制你的应用程序访问由您的应用程序创建或打开的文件,当你应该使用https://www.googleapis.com/auth/drive,它可以完全控制你的应用程序。

+0

是的,这是正确的...我是新的谷歌驱动器SDK ...谢谢你的回答... – 2013-04-06 12:06:58

+1

@PrabhatSubedi:Google最近有什么变化...改变建议的范围仍然只列出由我的应用程序创建的文件。 http://stackoverflow.com/questions/27759077/drive-sdk-not-listing-all-my-files – 2015-01-06 03:44:35

+0

@llamawithabowlcut目前无法在Google云端硬盘中工作...更好地查看SDK的文档和版本。 – 2015-01-06 05:19:38

0

我构建了我们都在寻找的功能。以下列出了Google云端硬盘文件夹中的所有文件和文件夹。您可以指定是否需要目录中的所有文件,或者只需要该目录中服务拥有的文件。天气你只需要id,或者如果你只想要文件名或者如果你想要两个。

Library_GoogleDriveFileList:

import numpy 
import pprint 
from apiclient import errors 
import collections 

def Main(
    Service = None, 
    FolderId = None, 
    ResultFileNamesOnly = False, 
    ResultFileIdsOnly = True, 
    IncludeServiceFilesOnly = False, 
    SortResults = False, 
    ResultFormat = None, 
    ): 

    Result = None 

    if (ResultFormat is None): 
     ResultFormat = 'list' 

    if (FolderId == None): 
     FolderId = 'root' 

    SearchParameterString = "'" + FolderId + "' in parents" 

    SearchOwners = None 
    if (IncludeServiceFilesOnly): 
     SearchOwners = 'DEFAULT' 
    else: 
     SearchOwners = 'DOMAIN' 

    print 'SearchOwners', SearchOwners 

    #Because there is a return limit of 460 files (falsly documented as 1000) 
    # we need to loop through and grab a few at a time with different requests 
    DriveFileItems = [] 
    PageToken = None 
    while True: 
     try: 
      print 'PageToken', PageToken 
      DriveFilesObject = Service.files().list(
       q   = SearchParameterString, 
       corpus  = SearchOwners, #'DOMAIN' 
       maxResults = 200, 
       pageToken = PageToken, 
       ).execute() 

      DriveFileItems.extend(DriveFilesObject['items']) 
      PageToken = DriveFilesObject.get('nextPageToken') 
      if not PageToken: 
       break 
     except errors.HttpError, error: 
      print 'An error occurred: %s' % error 
      break 


    #print 'DriveFileItems', DriveFileItems 
    #pprint.pprint(DriveFileItems) 

    FileNames = [] 
    FileIds = [] 
    for Item in DriveFileItems: 
     FileName = Item["title"] 
     FileNames.append(FileName) 

     FileId = Item["id"] 
     FileIds.append(FileId) 

    #print 'FileIds' 
    #pprint.pprint(FileIds) 


    if ResultFileNamesOnly == False and ResultFileIdsOnly == False: 


     if (ResultFormat == 'dict'): 
      Result = collections.OrderedDict() 
      for FileName , FileId in zip(FileNames, FileIds): 
       Result [FileName] = FileId 
     elif (ResultFormat == 'list'): 
      Result = numpy.array([FileIds, FileNames]).T.tolist() 

    elif ResultFileNamesOnly == True and ResultFileIdsOnly == False: 
     Result = FileNames 

    elif ResultFileNamesOnly == False and ResultFileIdsOnly == True: 
     Result = FileIds 

    elif ResultFileNamesOnly == True and ResultFileIdsOnly == True:  
     raise Exception('ResultFileNamesOnly == True and ResultFileIdsOnly == True:') 

    if (ResultFormat == 'list'):#type(Result).__name__ == 'list'): 
     Result = list(reversed(Result)) 

    if (SortResults): 
     Result = list(sorted(Result)) 
     #TODO -> more intellegent sort for dict, 2d list 

    return Result 
+0

我无法删除我自己的答案 - 这属于不同的页面:http://stackoverflow.com/questions/11730454/clarification-on-maxresults-and-nextpagetoken-using-google-drive-api-v2/39417284 #39417284 – 2016-09-09 18:01:42

+0

此解决方案也是python--增加了它的无关性 – 2016-09-09 18:02:18

相关问题