如何使用google驱动器中的files:list从文件夹中获取文件列表

时间:2016-05-05 14:16:25

标签: google-drive-api

我之前没有谷歌硬盘的经验。我需要获取google驱动器文件夹中的文件列表并获取文件的详细信息。

示例:

Drive  
     |--Folder  
             |-File1
             |-File2
     |--File3
     |--File4

我需要文件1,2,3,4的所有文件的详细信息。我可以使用以下代码获取File3和File4的详细信息:

def main():
    """Shows basic usage of the Google Drive API.

    Creates a Google Drive API service object and outputs the names and IDs
    for up to 10 files.
    """
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    param['q']="'root' in parents"
    service = discovery.build('drive', 'v2', http=http)
    results = service.files().list(**param).execute()
    page_token = None
    items = results.get('items', [])
    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            keys = list(item.keys())
            print('{0} ,{1} ,{2}, {3}, {4}, {5}, {6}'.format(item['title'],
                                                    item['createdDate'],
                                                    item['lastModifyingUserName'],
                                                    item['modifiedByMeDate'],
                                                    item['modifiedDate'],
                                                    item['ownerNames'],
                                                    item['mimeType']))

来自google api的快速入门指南。

我是否知道如何获取文件夹中File1,File2的详细信息,如上所示?

2 个答案:

答案 0 :(得分:1)

您应该将files.list与父查询一起使用。例如:

GET https://www.googleapis.com/drive/v2/files?q='BB0CHANGEDIDF5waGdzbUQ5aWs'+in+parents&key={YOUR_API_KEY}

您可以使用files.list方法搜索或过滤文件,该方法接受q参数,该参数是组合了一个或多个搜索子句的搜索查询。每个搜索条款由三部分组成。

您还可以查看此tutorial和相关的SO question

答案 1 :(得分:0)

您只需删除该行

即可
param['q']="'root' in parents"

然后你将获得所有文件。当我说"所有"文件,包括已删除的文件。要仅获取未删除的文件,您可以添加行

param['q']="trashed = false"