简体   繁体   English

如何使用服务帐户将共享谷歌驱动器中的文件移至垃圾箱

[英]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.使用我的个人帐户(也作为内容管理器),我可以毫无问题地删除文件。

Requirements要求

If you clearly understand how to impersonate an account you can skip to the Solution step.如果您清楚地了解如何模拟帐户,则可以跳至Solution步骤。

Solution解决方案

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参数列表文件includeItemsFromAllDrivessupportsAllDrives 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部分。

Reference参考

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.

相关问题 如何使用 Google Drive API 通过服务帐户访问域中的共享文件? - How to get access to shared files in a domain with a service account using the Google Drive API? 如何使用服务帐户凭据将文件上传到Google云端硬盘 - How to upload file to google drive with service account credential Google Drive API 使用 python 中的服务帐户未显示与我共享的文件夹 - Google Drive API using Service Account in python not showing up folders that are shared to me 使用共享设备和服务帐户获取文件夹和文件 Google Drive API - Get folder and files Google Drive API with Shared Device and Service Account 共享的 Google 驱动器突然返回 404 与服务帐户 - Shared Google drive suddenly returning 404 with service account 如何从colab中的驱动器垃圾中删除文件 - How to remove the file from trash in drive in colab Google Drive API 服务帐户身份验证以使用 Python 上传图像文件并将其插入到复制的文档中 - Google Drive API Service Account authentication to upload and insert an image file to into copied doc using Python Google云端硬盘api和服务帐户 - Google drive api and service account 使用服务帐户密钥的 Google Drive Python API 授权 - Google Drive Python API authorization using service account key 使用服务帐户模拟 Google Drive 获取刷新令牌 - Get refresh token using service account impersonation Google Drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM