我正在遵循有关如何在成功进行身份验证后执行upload files to Google Drive using Python的说明(如here所述)。
我使用...加载必要的库。
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools..and在尝试运行我自己的上传时,我被告知...
NameError: name 'MediaFileUpload' is not defined任何意见或建议都将非常受欢迎。
发布于 2018-06-22 16:29:42
以下是有关如何使用Wesley Chun's blogpost中的Python2和Python3在Drive API中上传/下载的完整脚本
#!/usr/bin/env python
from __future__ import print_function
import os
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))
FILES = (
('hello.txt', False),
('hello.txt', True),
)
for filename, convert in FILES:
metadata = {'title': filename}
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks').execute()
if res:
print('Uploaded "%s" (%s)' % (filename, res['mimeType']))
if res:
MIMETYPE = 'application/pdf'
res, data = DRIVE._http.request(res['exportLinks'][MIMETYPE])
if data:
fn = '%s.pdf' % os.path.splitext(filename)[0]
with open(fn, 'wb') as fh:
fh.write(data)
print('Downloaded "%s" (%s)' % (fn, MIMETYPE))https://stackoverflow.com/questions/50971697
复制相似问题