【问题标题】:Download file from google share drive using pydrive, file exist, but API return 404 File not found error使用 pydrive 从 google 共享驱动器下载文件,文件存在,但 API 返回 404 File not found 错误
【发布时间】:2020-01-08 17:38:26
【问题描述】:

我正在使用 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."
 }
}

【问题讨论】:

    标签: python pydrive


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多