@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"
}
}
最终一致?