[英]How to move a file in a shared google drive to trash using a service account
I use a service account that is content-manager.我使用的是内容管理器的服务帐户。 I have no problem uploading files to the shared drive using the drive-api from python.
我使用 python 中的 drive-api 将文件上传到共享驱动器没有问题。 Using
使用
service.files().list(q="name='file_name'", fields="files(id)").execute()
I obtain the file_id from my code.我从我的代码中获取了 file_id。 This file_id is the right one based on the link to the file.
这个 file_id 是基于文件链接的正确的。
When I perform the following statement:当我执行以下语句时:
response = service.files().update(fileId=file_id, body={'trashed': True}).execute()
I get a我得到一个
404: file not found.
404:未找到文件。
How to resolve this ?如何解决这个问题? With my personal account (also as content-manager) I have no problems trashing the file.
使用我的个人帐户(也作为内容管理器),我可以毫无问题地删除文件。
If you clearly understand how to impersonate an account you can skip to the Solution
step.如果您清楚地了解如何模拟帐户,则可以跳至
Solution
步骤。
First of all you will need a service account首先,您需要一个服务帐户
Delegate domain-wide authority委派全域权限
Make sure that you are correctly calling the API确保您正确调用 API
By default Python Google Drive API client V3 doesn't include shared drive files, that's why you have to explicitly pass a parameter supportsAllDrives
and set it to True and before that you should list your files in order to know the fileId parameter by using includeItemsFromAllDrives
and supportsAllDrives
.默认情况下, Python的谷歌云端硬盘API客户端V3不包括共享驱动器的文件,这就是为什么你必须明确地传递参数
supportsAllDrives
并将其设置为True而在这之前,你应该为了利用知道的fileid参数列表文件includeItemsFromAllDrives
和supportsAllDrives
。 Here's an example to list all the files in all your drives and how to trash a file in a Shared Drive using a service account:下面是一个示例,用于列出所有驱动器中的所有文件以及如何使用服务帐户将共享驱动器中的文件删除:
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)
Otherwise if you already know the fileId just use the update
part.否则,如果您已经知道fileId ,请使用
update
部分。
Python Google Drive API client V3 > Update a file Python Google Drive API 客户端 V3 > 更新文件
Google Identity Platform > Impersonate a user by using a service account Google Identity Platform > 使用服务帐号模拟用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.