我看到這是一個相當古老的帖子,但我只是遇到了同樣的問題,試圖通過curl使用Google Drive API並最終找到了解決方案,但使用起來有點棘手,特別是在內容長度計算上,所以我如果有人幫助別人,會留下回復!
根據谷歌文檔,首先,你應該在主請求中包含Content-Type,Content-Length和Authorization標頭。對於內容長度,你可以讓嫋嫋自動計算,所以添加其他標題是這樣的:
-H "Content-Type: multipart/related; boundary=287032381131322"
-H "Authorization: Bearer <Your Oauth2 Access Token>"
其次,你需要包括與元數據的多段。這部分需要開始與邊界,其次是Content-Type頭:
--287032381131322
Content-Type: application/json; charset=UTF-8
其次是元數據內容:
{\"name\":\"Test\"}
下一節,對於二進制數據,邊界開始其必須再次包含邊界定界符和Content-Type頭:
--287032381131322
Content-Type: application/octet-stream
其次你要上傳的二進制數據,這簡直是爲傳遞:
--data-binary @file.ext
最後,你應該包括關閉邊界分隔符一個新的部分:在我的例子
--287032381131322--
因此,使用的5679個字節的文件,這將是完整的命令和應答谷歌:
curl -v -H "Content-Type: multipart/related; boundary=287032381131322" \
-H "Authorization: Bearer <My Oauth2 Access Token>" \
--data-binary $'--287032381131322\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n{\"name\":\"Test\"}\r\n--287032381131322\r\nContent-Type: application/octet-stream\r\n\r\n' \
--data-binary @b0c11d3b40b71ed08108e5dad7f6ecee-0 \
--data-binary $'\r\n--287032381131322--' \
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
> POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
> Host: www.googleapis.com
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: multipart/related; boundary=287032381131322
> Authorization: Bearer <My Oauth2 Access Token>
> Content-Length: 5848
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< X-GUploader-UploadID: <My Uploader 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: Mon, 24 Jul 2017 20:56:50 GMT
< Content-Length: 123
< Server: UploadServer
< Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"
<
{
"kind": "drive#file",
"id": "0B-4U01-IEMlATjdfbVNqQURiN0U",
"name": "Test",
"mimeType": "application/octet-stream"
}
* Connection #0 to host www.googleapis.com left intact
不成功,我使用此代碼,但不是成功。爲什麼?服務器是谷歌驅動器。 – xxxxxxx 2014-09-24 00:50:27
-F選項顯式用於「multipart/form-data」,其中OP要求「multipart/related」。 (和谷歌驅動器要求'多部分/相關') – NobodysNightmare 2015-05-29 07:34:38