python - 无法在 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()
推荐阅读
- reactjs - 当使用 Material UI 切换深色模式时,Formio 表单的字体颜色更改为白色
- apache-spark - 如何在 Azure Synapse WorkSpace 中找到并安装 Great_Expectations .JAR 文件
- reactjs - TinyMCE React 光标在键入一个键后重新开始
- c++ - 处理完所有排队事件后调用代码
- python - 如何遍历嵌套字典(来自 json)并检查键是否在另一个嵌套字典中(来自 json),如果不是则添加?
- amazon-web-services - 如何通过 NAT 网关而不是公共 IP 传输数据包?
- openssl - 在 OpenSSL 中测试自生成的 RSA 公钥
- javascript - 尝试导入错误:“Jumbotron 未从“react-bootstrap”导出(导入为“Jumbo”)
- c++ - 如何检查 C++ 类是否不完整(仅声明)?
- c# - 如何在使控制台应用程序从 appsettings 文件中读取数据时修复 AddJsonFile 方法中的错误