gpt4 book ai didi

python - 用 Python 清空 Google Drive Trash

转载 作者:行者123 更新时间:2023-12-05 01:17:44 24 4
gpt4 key购买 nike

我正在尝试使用 API 清空我的 Google Drive 垃圾桶,但没有成功。我尝试使用 requests 库,脚本运行没有错误,但驱动器垃圾没有被删除。我已经能够通过 API 获取文件,但不能删除或清空垃圾箱。似乎我应该能够只发送一个 http 请求,但这对我来说比看起来要困难得多。任何建议将不胜感激。这就是我所拥有的。我什至没有收到 json 响应。

from __future__ import print_function
import httplib2
import os
import requests

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage


try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'C:\Scripts\Link Expiration\client_secret.json'
APPLICATION_NAME = 'Drive API Delete Trash'

def get_credentials():
"""Gets valid user credentials from storage.

If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.

Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')

store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials

def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)

r = requests.delete('https://www.googleapis.com/drive/v2/files/trash')
r.json()

if __name__ == '__main__':
main()

最佳答案

这个修改怎么样?

def main():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http) # you can also use at 'v3'.

service.files().emptyTrash().execute() # Added

如果我误解了你的问题,我很抱歉。

关于python - 用 Python 清空 Google Drive Trash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48820510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com