1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年5月16日 @author: debo.zhang ''' #!/usr/bin/env python
from __future__ import print_function import os
from apiclient.discovery import build from httplib2 import Http from oauth2client import file, client, tools from googleapiclient.http import MediaFileUpload try: import argparse flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError: flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.file' store = file.Storage('storage.json') creds = store.get() if not creds or creds.invalid: 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, 'client_secret.json') flow = client.flow_from_clientsecrets(credential_path, SCOPES) creds = tools.run_flow(flow, store, flags) \ if flags else tools.run(flow, store)
http = creds.authorize(Http()) DRIVE = build('drive', 'v3', http=http) # DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
FILES = ( ('tmp.jpg', False), )
for filename, convert in FILES: file_metadata = { 'name' : 'tmp.jpg' } media = MediaFileUpload('tmp.jpg', mimetype='image/jpeg') res = DRIVE.files().create(body=file_metadata, media_body=media, fields='id').execute() print(res)
|