使用pydrive从Google共享驱动器下载文件,文件存在,但API返回404文件未找到错误

时间:2020-01-08 17:38:26

标签: python pydrive

我正在使用python从团队拥有的Google共享驱动器中获取文件。当前的问题是我可以解析所有文件ID,但是无法使用pyDrive下载或读取文件内容。

首先,我生成了一个“驱动器”:

from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
from pprint import pprint

gauth = GoogleAuth()
# Create local webserver and auto handles authentication.
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

然后我使用文件ID来获取内容:

file_id = 'xxx'
file_ = drive.CreateFile({'id': file_id})
content = file_.GetContentString() # <--------------problem line
print(content)

我得到的错误是:

pydrive.files.ApiRequestError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/xxx?alt=json returned "File not found: xxx">

但是,当我转到https://www.googleapis.com/drive/v2/files/xxx?alt=json时文件确实存在,它返回:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

我不知道发生了什么。请帮忙! 谢谢,

1 个答案:

答案 0 :(得分:1)

使用什么文件ID,您需要查看pydrive文档以进行下载,这很奇怪,因为您必须上传文件才能实际下载

您似乎还使用GetContentString,它只能获取字符串,可以使用GetContentFile获取所有类型的文件

下面的代码将下载您需要尝试的文件

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

#1st authentification
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles
#authentication.
drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.Upload()

file6 = drive.CreateFile({'id' : file1['id']})
file6.GetContentFile('enter the file name you want to download here')

file1.Delete()