2017-07-31 89 views
1

我按照https://developers.google.com/drive/v3/web/resumable-upload上的说明执行了可恢复文件上载。GDrive可恢复上传响应中缺少位置头文件

我有一个服务帐户工作令牌(我确认我可以列出文件),并做

$ 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"}' 

我应该得到一个空的响应,并使用上传我的文件的URL Location头。相反,我没有得到任何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和用药剂的HTTPotion ,结果相同。有人知道我在做什么错吗?

回答

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" 
} 
0

@noogui answer不是解决方案,因为他/他没有注意到s /他为同一个操作提供了两个不同的URL。但是看到这两个网址并拢使我意识到,谷歌的文档是错误的,因为它在文本中使用此:

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

,并用这个例子:

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

第一个是正确的,第二个是错误的。我希望有人从谷歌读取并修复文档。

我可能会说,使用不同的URL进行文件上传和所有其他文件操作是不好的设计。证明:即使Google在示例中选择了一个,也犯了一个错误。

这是正确的回应,我与/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" 

Location头是那里的响应。

我现在可以上传文件:

$ 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" 
} 
} 

最终一致?