首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应中缺少数据json结果

响应中缺少数据json结果
EN

Stack Overflow用户
提问于 2021-04-09 19:22:51
回答 1查看 176关注 0票数 0

在google站点上测试api之后,我正在测试Google驱动API V3 files.list方法。

试试看我 I收到了预期的结果。

代码语言:javascript
复制
{
 "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时,我忽略了这个部分:

代码语言:javascript
复制
 "kind": "drive#fileList",
 "nextPageToken": "~!!~AI9......",
 "incompleteSearch": false,
 "files": 

这是我的密码:

代码语言:javascript
复制
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))

结果:

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

方法GdriveConnection()

代码语言:javascript
复制
    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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-09 20:10:00

解释:

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

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

参考:

Python中的get()函数

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67027120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档