首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用google-api-python-client时出错

使用google-api-python-client时出错
EN

Stack Overflow用户
提问于 2018-06-21 22:58:52
回答 1查看 186关注 0票数 0

我正在遵循有关如何在成功进行身份验证后执行upload files to Google Drive using Python的说明(如here所述)。

我使用...加载必要的库。

代码语言:javascript
复制
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

..and在尝试运行我自己的上传时,我被告知...

代码语言:javascript
复制
NameError: name 'MediaFileUpload' is not defined

任何意见或建议都将非常受欢迎。

EN

回答 1

Stack Overflow用户

发布于 2018-06-22 16:29:42

以下是有关如何使用Wesley Chun's blogpost中的Python2和Python3在Drive API中上传/下载的完整脚本

代码语言:javascript
复制
#!/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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50971697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档