【问题标题】:Missing data in response json results响应 json 结果中缺少数据
【发布时间】:2021-04-09 19:22:51
【问题描述】:

我在 Google 网站上测试 API 后正在测试 google drive api V3 files.list 方法

Try me我收到了预期的结果。

{
 "kind": "drive#fileList",
 "nextPageToken": "~!!~AI9......",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1HL...",
   "name": "Slack Channel DLs",
   "mimeType": "application/vnd.google-apps.folder",
   "teamDriveId": "0AD...",
   "driveId": "0AD..."
  }]
}

但是在使用 python 时,我缺少这部分:

 "kind": "drive#fileList",
 "nextPageToken": "~!!~AI9......",
 "incompleteSearch": false,
 "files": 

这是我的代码:

import json


from main_methods import GdriveConnection
# mainmethods is a script for my methods.

googleDrive = GdriveConnection()


files = googleDrive.listFiles()

print(json.dumps(files, indent=2))
count = 0

for file in files:
    count = count + 1

print('\n Total files: ' + str(count))

结果:

[
  {
   "kind": "drive#file",
   "id": "1HL...",
   "name": "Slack Channel DLs",
   "mimeType": "application/vnd.google-apps.folder",
   "teamDriveId": "0AD...",
   "driveId": "0AD..."
  }]

方法 GdriveConnection()

    def listFiles(self):
        service = build('drive', 'v3', credentials=self.creds)

        results = service.files().list(corpora='user', includeItemsFromAllDrives='true', orderBy='folder', pageSize='1000', supportsAllDrives='true', supportsTeamDrives='true').execute()

        files = results.get('files', [])

        return(files)

【问题讨论】:

    标签: python google-api google-drive-api


    【解决方案1】:

    解释:

    files = results.get('files', [])返回整个响应的files对象,应该在上一行的results中。

    要打印整个响应,请返回 results 而不是 files

    参考:

    get() function in Python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 2018-03-12
      相关资源
      最近更新 更多