Incomplete Google Drive REST API results for Team Drive files

家住魔仙堡 提交于 2019-12-11 05:07:55

问题


Problem

During the first phase of my Team Drive backup system, I first need to scan files in the given Team Drive in order to determine which files to copy for backup.

I (think?) have full permissions over the files & folders of the Team Drive, due to the credentials I use (set to Owner in the cloud console).

Nevertheless, my problem is that when I query the REST API for a file listing of a given Team Drive, the results do not comply with the documentation. The returned file objects only contain 5 fields that are:

  • kind, name, id, mimeType, teamDriveId

According to the documentation provided, I should receive a handful more of fields.

Below is the code I use to query the API and the output.

Simplified source

credentials = get_credentials() # retrieves and processes app credentials
drive = get_drive_api(credentials) # get the drive API v3 using httplib2 and discovery
query = drive.files().list(
    pageSize = 10,
    corpora = 'teamDrive',
    supportsTeamDriveItems = True,
    includeTeamDrives = True,
    teamDriveId = "..."
)
results = query.execute() # contact the REST API
files = results.get('files', [])
for file in files:
    print(file)

For the given Team Drive, output is

{
  'kind': 'drive#file',
  'id': '...',
  'name': 'filename',
  'mimeType': 'application/vnd.google-apps.document',
  'teamDriveId': '...'
}

Which clearly isn't the expected output according to the docs.

Any insights on the reason I do not get the complete expected data ?


回答1:


With Google Drive API v3 full resources are no longer returned by default. Use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.

To fetch all available fields of the resource you can set fields to *.

For example:

query = drive.files().list(
    pageSize = 10,
    corpora = 'teamDrive',
    supportsTeamDriveItems = True,
    includeTeamDrives = True,
    teamDriveId = "...",
    fields="*"  # or maybe "files(id,name,capabilities/canShare),nextPageToken"
)


来源:https://stackoverflow.com/questions/46180485/incomplete-google-drive-rest-api-results-for-team-drive-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!