响应 json 结果中缺少数据
Missing data in response json results
在 Google 站点 Google 上测试 API 之后,我正在测试 google 驱动器 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)
解释:
files = results.get('files', [])
return是整个响应的files
对象,应该在上一行的results
中。
要打印整个响应,return results
而不是 files
。
参考:
在 Google 站点 Google 上测试 API 之后,我正在测试 google 驱动器 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)
解释:
files = results.get('files', [])
return是整个响应的files
对象,应该在上一行的results
中。
要打印整个响应,return results
而不是 files
。
参考: