【问题标题】:How can I get the Drive API to print a description of the file?如何让 Drive API 打印文件描述?
【发布时间】:2016-03-23 12:51:49
【问题描述】:

这是来自 Google Drive v3 API 示例的代码。

定义主(): """显示 Google Drive API 的基本用法。""" 凭证 = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v3', http=http)

results = service.files().list(q="mimeType='image/jpeg'",
    pageSize=2,fields="nextPageToken, files(id, mimeType, name, description)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], item['description']))

我添加了看起来像 v3 文档中的有效属性的描述。我收到此错误。

print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], item['description']))

KeyError: '描述'

我正在使用 Python 3.0。原始示例中的代码可以在这里找到 - https://developers.google.com/drive/v3/web/quickstart/python#step_3_set_up_the_sample

文件参考的文档可以在这里找到 - https://developers.google.com/drive/v3/reference/files

【问题讨论】:

    标签: python-3.x google-drive-api google-api-python-client


    【解决方案1】:

    虽然描述有效,但并非所有文件都有描述,如果没有描述,则不会返回描述属性。试试:

    for item in items:
      description = item.get('description', '')  
      print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], description))
    

    如果设置了描述,则将描述设置为 API 返回的属性;如果没有为项目设置属性,则设置为 ''(空白)。

    【讨论】:

    • 感谢您让我开始!我想知道为什么驱动api默认不返回空字符串。
    猜你喜欢
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多