Python 如何使用服务帐户将共享google驱动器中的文件移动到垃圾箱

Python 如何使用服务帐户将共享google驱动器中的文件移动到垃圾箱,python,google-api,google-drive-api,google-api-python-client,shared-drive,Python,Google Api,Google Drive Api,Google Api Python Client,Shared Drive,我使用的服务帐户是content manager。我可以使用python中的驱动器api将文件上传到共享驱动器。使用 service.files().list(q=“name='file\u name',fields=“files(id)”).execute() 我从我的代码中获取文件\u id。根据文件的链接,此文件的id是正确的 当我执行以下语句时: response=service.files() 我得到一份工作 404:找不到文件 如何解决这个问题?使用我的个人帐户(也作为内容管理器

我使用的服务帐户是content manager。我可以使用python中的驱动器api将文件上传到共享驱动器。使用

service.files().list(q=“name='file\u name',fields=“files(id)”).execute()
我从我的代码中获取文件\u id。根据文件的链接,此文件的id是正确的

当我执行以下语句时:

response=service.files()
我得到一份工作

404:找不到文件

如何解决这个问题?使用我的个人帐户(也作为内容管理器),我可以轻松地销毁该文件。

如果您清楚了解如何模拟帐户,您可以跳到
解决方案
步骤。

  • 首先,你需要一个

  • 委派

  • 确保你的回答正确

解决方案 默认情况下不包括共享驱动器文件,这就是为什么您必须显式地传递参数
supportsAllDrives
,并将其设置为True,在此之前,您应该使用
includeItemsFlomaldrives
supportsAllDrives
列出您的文件,以便知道fileId参数。以下示例列出了所有驱动器中的所有文件,以及如何使用服务帐户在共享驱动器中丢弃文件:

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = './service_account_key.json'

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# Impersonate user@example.com account in my example.com domain
delegated_credentials = credentials.with_subject('user@example.com')

# Use the delegated credentials to impersonate the user
service = build('drive', 'v3', credentials=delegated_credentials)

# List all the files in your Drives (Shared Drives included)
results = service.files().list(fields="nextPageToken, files(id, name, trashed)", includeItemsFromAllDrives=True, supportsAllDrives=True).execute()
items = results.get('files', [])

if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print(u'{0} ({1}) - Trashed? {2}'.format(item['name'], item['id'], item['trashed']))

# Use the filedId in order to trash your shared file
response = service.files().update(fileId=fileId, body={'trashed': True}, supportsAllDrives=True).execute()
print(response)
否则,如果您已经知道文件ID,只需使用
更新部分即可

参考文献


您使用的是哪个驱动器API版本?@JoseVasquez我使用的是驱动器API v3