java - 在java中,Google没有列出谷歌驱动上传的文件

  显示原文与译文双语对照的内容
143 4

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

下面是列出 file:的方法


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();


 }



谁能帮我? 请提前致谢。

时间: 原作者:

138 5

我想你使用的是错误的oauth作用域,可能 https://www.googleapis.com/auth/drive.file 当你应该使用应用程序时,它限制你的应用程序对创建或者打开的文件的访问权限 https://www.googleapis.com/auth/drive 它可以完全控制你的应用。

原作者:
57 4

我建立了我们一直在寻找的功能。 以下列出了 google drive文件夹中的所有文件和文件夹。 你可以指定是否希望目录中的所有文件或者服务在目录中所拥有的文件。 只要你想要的是,或者你想要的文件名,或者你想要这两者的天气。

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



原作者:
...