如何解决无法在python中使用Google Drive API上传文件
我想使用Python中的Google Drive API将文件从本地文件夹上传到Google Drive。
当前,我遇到此错误:
Traceback (most recent call last):
File "c:\Users\ice\projects\testproject\googledrive_api.py",line 72,in <module>
service.files().create(
File "C:\Users\ice\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googleapiclient\_helpers.py",line 134,in positional_wrapper
return wrapped(*args,**kwargs)
File "C:\Users\ice\AppData\Local\Programs\Python\Python38-32\lib\site-packages\googleapiclient\http.py",line 915,in
execute
raise HttpError(resp,content,uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=multipart returned "File not found: 18JvVkD3wy79bZ039viXEt1J2AP3f5Iqm.">
这是我的代码:
import os
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials
from Google import Create_Service
from googleapiclient.http import MediaFileUpload
# Authentication and list files in Google Drive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
fileList = drive.ListFile(
{'q': "'1Gnv3rIbBG1-xwtk6PIidqB02XkeoUEvc' in parents and trashed=false"}).GetList()
for file1 in fileList:
print('title: %s,id: %s' % (file1['title'],file1['id']))
# Upload file(s) to Google Drive
CLIENT_SECRET_FILE = 'client_secrets.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']
service = Create_Service(CLIENT_SECRET_FILE,API_NAME,API_VERSION,SCOPES)
# ID of the destination folder
folder_id = '18JvVkD3wy79bZ039viXEt1J2AP3f5Iqm'
# 1st list for file names,2nd list for file types
file_names = ['age.pdf','age.png']
mime_types = ['application/pdf','image/png']
for file_name,mime_type in zip(file_names,mime_types):
file_metadata = {
'name': file_name,'parents': [folder_id]
}
media = MediaFileUpload('./{0}'.format(file_name),mimetype=mime_type)
service.files().create(
body=file_metadata,media_body=media,fields='id'
).execute()
我真的不知道如何解决这个问题。我感谢您的帮助。谢谢。
解决方法
在创建函数中使用此 supportsTeamDrives = True 参数
赞
service.files().create(
supportsTeamDrives=True,body=file_metadata,media_body=media,fields='id'
).execute()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。