我構建了我們都在尋找的功能。以下列出了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
我有同樣的問題。你能告訴我如何解決它?我與它連接數天 – Dolphin 2013-07-19 09:03:36
更改oauth範圍到'https:// www.googleapis.com/auth/drive' – 2013-07-19 09:17:00
我正在使用java android。我不知道在哪裏改變這個範圍。這裏是我的代碼,你可以看看http://pastebin.com/pTFaVs4A – Dolphin 2013-07-19 09:29:19