我在 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)
答案 0 :(得分:2)
files = results.get('files', [])
返回整个响应的 files
对象,它应该在上一行的 results
中。
要打印整个响应,返回 results
而不是 files
。
参考: