【发布时间】:2018-02-16 05:21:32
【问题描述】:
我正在尝试使用 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()
【问题讨论】:
标签: python google-api google-drive-api google-api-python-client