Google云端硬盘可恢复上传失败

时间:2015-01-29 19:16:23

标签: python google-drive-api python-requests

我正在尝试使用Google Drive resumable upload api [https://developers.google.com/drive/web/manage-uploads#resumable]上传文件,并且我总是在流程的第3步中获得带有无效上传请求的400状态代码。

对于第1步(Starting a resumable session),我得到会话uri,当我上传内容时,我收到了错误的请求错误。

REQUEST HEADERS:
{
  "X-Upload-Content-Length": 249159,
  "X-Upload-Content-Type": "application/pdf",
  "content-type": "application/json",
  "Authorization": "Bearer ya..."
}

REQUEST BODY:
{
  "parents": [
    {
      "id": "0B..."
    }
  ],
  "title": "New file.pdf"
}

我得到的回应是会议uri as https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=AEnB2UpNCcQgsbuqKer6z25jVRX4ijweQkpAJ1aQ1nEQPGi_ODtvob9vSwQk2oyjzyUNQs-b2HenMst92HQo6WMKmQyO1lgTww

对于第3步(Uploading the file) 以下是请求标题。

{
  "Content-Length": 249159,
  "Accept-Encoding": "gzip, deflate",
  "Accept": "*/*",
  "User-Agent": "python-requests/2.3.0 CPython/2.7.8 Darwin/14.0.0",
  "Content-Range": "bytes 0-249159/249159",
  "Content-Type": "application/pdf",
  "Authorization": "Bearer ya..."
}

请求正文包含长度再次相同的数据。数据是使用python请求模块从另一个URL获取的块。

以下是对谷歌上述请求的回复

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "badRequest",
        "message": "Invalid Upload Request"
      }
    ],
    "code": 400,
    "message": "Invalid Upload Request"
  }
}

除Google Drive外,它适用于其他云(相同的方法)。我在这里缺少什么?

1 个答案:

答案 0 :(得分:1)

我终于自己钉了它。这是代码中的一个错误,其中最后一个块的内容范围偏离了1个字节。

相关问题