可能这是一个重复的问题,但我没有得到任何成功的答案。
我正在尝试将一些文件上传到谷歌硬盘。它成功上传但文件名仍然是"无标题"。以下是我在curl和python请求中尝试过的内容
卷曲
curl -X POST -H 'Content-Type:*/*'
-H 'Authorization: Bearer <The access token>'
-H 'Content-Length:125'
--upload-file 'foo.txt'
'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart'
-d '{"title":"foo.txt","description":"test file"}'
这里尝试使用Content-Type:multipart/related; boundary="foo_bar_baz"
投掷错误说&#34;使用multipart&#34;而只尝试&#34; multipart&#34;它说使用&#34; / &#34; 。任何文件如何上传,但名称是&#34;无标题&#34;
以下是python请求中的相同内容:
upload_files = requests.post(
'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart',
headers={'Authorization':'Bearer {}'.format(access_token),
'Content-Type':'*/*',
'Content-Length':file_size},
data={"title":file_name,
"mimeType": "*/*",
"parents": [{"id":"root"}],
"description": "mypdf.pdf"},
files={'file':file},
)
请不要贬低它,如果我犯了一个愚蠢的错误,而是发表评论。我会检查它。在我的情况下我不能使用谷歌驱动器客户端 非常感谢您抽出时间来检查我的问题。
答案 0 :(得分:0)
您没有正确构建多部分正文。有关示例,请参阅https://developers.google.com/drive/web/manage-uploads#multipart
POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: number_of_bytes_in_entire_request_body
--foo_bar_baz
Content-Type: application/json; charset=UTF-8
{
"title": "My File"
}
--foo_bar_baz
Content-Type: image/jpeg
JPEG data
--foo_bar_baz--