【发布时间】:2020-10-24 06:36:33
【问题描述】:
我想使用 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()
我真的不知道如何解决这个问题。我感谢您的帮助。谢谢。
【问题讨论】:
-
这可能是因为您没有获得正确的文件夹 ID 或者您没有使用正确的令牌(授权问题)。您是否能够正确列出您的文件?您能否尝试删除您的令牌并再次运行脚本以获取新令牌以确保这不是问题?父文件夹是在共享云端硬盘中还是在您的普通云端硬盘中?
标签: python python-3.x google-api google-drive-api google-api-client