未解析的参考文件“ MediaFileUpload”

时间:2018-06-21 15:32:36

标签: python-3.x google-api-python-client

出现此错误时,我正在尝试在python中使用Google driveApi,我安装了所有必要的依赖项,但仍然遇到此问题,以供未解决的参考使用:

Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/dandalo/GoogleDriveAPI.py", line 2, in 
   <module>
    class UploadToGoogleDrive:
File "C:/Users/User/PycharmProjects/dandalo/GoogleDriveAPI.py", line 20, in 
UploadToGoogleDrive
    media = googleapiclient.MediaFileUpload('files/photo.jpg',
AttributeError: module 'googleapiclient' has no attribute 'MediaFileUpload'

这是我的代码,我在做什么错了?

class UploadToGoogleDrive:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools


# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('credentials.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)
service = build('drive', 'v3', http=creds.authorize(Http()))

file_metadata = {'name': 'photo.jpg'}
media = MediaFileUpload('files/photo.jpg',
                        mimetype='image/jpeg')
file = drive_service.files().create(body=file_metadata,
                                    media_body=media,
                                    fields='id').execute()
print('File ID: %s' % file.get('id'))

2 个答案:

答案 0 :(得分:1)

只需将其添加到您的代码中

from apiclient.http import MediaFileUpload

答案 1 :(得分:1)

您正在尝试从googleapiclient导入MediaFileUpload。

MediaFileUpload来自googleapiclient

中的http模块

试试看 media = googleapiclient.http.MediaFileUpload('files/photo.jpg')