GDrive可恢复上传响应中缺少位置标头

时间:2017-07-31 17:07:17

标签: google-drive-api

我按照https://developers.google.com/drive/v3/web/resumable-upload上的说明进行了可恢复的文件上传。

我有一个服务帐户的工作令牌(我验证了我可以列出文件)并且

$ curl -i -H "Authorization: Bearer <token>" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "Content-Length: 50" \
-H "X-Upload-Content-Type: application/pdf" \
-H "X-Upload-Content-Length: 1560010" -X POST \
'https://www.googleapis.com/drive/v3/files?uploadType=resumable' \
-d '{"name": "myObject","mimeType": "application/pdf"}'

我应该获得一个空响应和一个Location头,其中包含用于上传文件的URL。相反,我没有获得Location头和指向新文件的响应主体

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Mon, 31 Jul 2017 10:40:37 GMT
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Transfer-Encoding: chunked

{
 "kind": "drive#file",
 "id": "0B8f3X4o60bqnWjU5aFIxbHB1bTA",
 "name": "myObject",
 "mimeType": "application/pdf"
}

列出文件的卷曲确认文件在那里:

$ curl -H "Authorization: Bearer <token>"  \
https://www.googleapis.com/drive/v3/files
...
{
"kind": "drive#file",
"id": "0B8f3X4o60bqnWjU5aFIxbHB1bTA",
"name": "myObject",
"mimeType": "application/pdf"
},
...

我尝试使用wget和Elixir的HTTPotion,结果相同。有人知道我做错了吗?

2 个答案:

答案 0 :(得分:1)

@noogui answer不是解决方案,因为他/她没有注意到他/她为同一操作提供了两个不同的网址。然而,看到这两个网址紧密相关,让我意识到Google的文档是错误的,因为它在文本中使用了这个:

 POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

以及示例中的内容:

 POST https://www.googleapis.com/drive/v3/files?uploadType=resumable

第一个是正确的,第二个是错的。我希望Google的某位读者能够阅读并修复文档。

我可能会说使用不同的URL进行文件上传和所有其他文件操作都是糟糕的设计。证明:即使谷歌选择在示例中使用的那个也是错误的。

这是我使用/upload网址得到的正确回复:

$ curl -i -H "Authorization: Bearer <token>"  \
-H "Content-Type: application/json; charset=UTF-8" \
-H "Content-Length: 50" \
-H "X-Upload-Content-Type: application/pdf" \
-H "X-Upload-Content-Length: 1560010" \
-X POST 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable'  \
-d '{"name": "myObject","mimeType": "application/pdf"}'
HTTP/1.1 200 OK
X-GUploader-UploadID: <upload-id>
Location: https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=<upload-id>
Vary: Origin
Vary: X-Origin
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Tue, 01 Aug 2017 06:37:02 GMT
Content-Length: 0
Server: UploadServer
Content-Type: text/html; charset=UTF-8
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"

位置标题位于回复中。

我现在可以上传文件:

$ curl -i -H "Authorization: Bearer <token>" \
-H "Content-Type: application/pdf" \
-H "Content-Length: 1560010" \
-X PUT 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=<upload-id>' \
--data-binary "@myObject.pdf"
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
X-GUploader-UploadID: <upload-id>
Vary: Origin
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Tue, 01 Aug 2017 06:48:47 GMT
Content-Length: 118
Server: UploadServer
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"

{
 "kind": "drive#file",
 "id": "<file-id>",
 "name": "myObject",
 "mimeType": "application/pdf"
}

证明它有效:

$ curl -H "Authorization: Bearer <token-id>" \
'https://www.googleapis.com/drive/v3/files/<file-id>?alt=media' \
> download.pdf
$ diff download.pdf myObject.pdf
$ echo $status
0

请注意,下载成功前需要几分钟。我在第一次尝试时遇到了这个错误:

HTTP/1.1 401 Unauthorized
Vary: X-Origin
WWW-Authenticate: Bearer realm="https://accounts.google.com/", error=invalid_token
Content-Type: application/json; charset=UTF-8
Date: Tue, 01 Aug 2017 06:56:35 GMT
Expires: Tue, 01 Aug 2017 06:56:35 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"
Accept-Ranges: none
Vary: Origin,Accept-Encoding
Transfer-Encoding: chunked

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

最终一致吗?

答案 1 :(得分:0)

尝试使用Initiating a resumable upload session指南中的此格式:

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable

示例:启动可恢复的上传会话

以下示例显示如何启动可恢复会话以上载新文件:

POST https://www.googleapis.com/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: image/jpeg
X-Upload-Content-Length: 2000000

{
  "name": "myObject"
}