1

我正在努力翻译这个命令(我可以成功运行)

curl -X POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: multipart/related" \
-F "metadata=@metadata.txt;type=application/json;charset=UTF-8" \
-F "file=@upload.jpg;type=image/jpeg"

使用 pycurl 到 python 脚本:

import pycurl
url = 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart'

c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.USERAGENT, 'Curl')
c.setopt(c.VERBOSE, 1)
c.setopt(c.URL, url)
c.setopt(c.HTTPHEADER, [
    'Authorization: Bearer %s' % str(ACCESS_TOKEN),
    'Content-Type: multipart/related',
])

data = [
     ('metadata', (c.FORM_FILE, 'metadata.txt')),
     ('type', 'application/json'),
     ('charset', 'UTF-8'),
     ('file', (c.FORM_FILE, 'upload.jpg')),
     ('type', 'image/jpeg'),
]
c.setopt(c.HTTPPOST, data)
c.perform()
c.close()

但是当我运行脚本时总是出现这个错误

* About to connect() to www.googleapis.com port 443 (#0)
*   Trying 74.125.132.95... * connected
* found 153 certificates in /etc/ssl/certs/ca-certificates.crt
*    server certificate verification OK
*    common name: *.googleapis.com (matched)
*    server certificate expiration date OK
*    server certificate activation date OK
*    certificate public key: RSA
*    certificate version: #3
*    subject: C=US,ST=California,L=Mountain View,O=Google Inc,CN=*.googleapis.com
*    start date: Thu, 27 Sep 2012 01:23:09 GMT
*    expire date: Fri, 07 Jun 2013 19:43:27 GMT
*    issuer: C=US,O=Google Inc,CN=Google Internet Authority
*    compression: NULL
*    cipher: ARCFOUR-128
*    MAC: SHA1
> POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
User-Agent: Curl
Host: www.googleapis.com
Accept: */*
Authorization: Bearer ACCESS_TOKEN
Content-Length: 43373
Expect: 100-continue
Content-Type: multipart/related; boundary=----------------------------b91f88a180be

* Done waiting for 100-continue
< HTTP/1.1 400 Bad Request
< Server: HTTP Upload Server Built on Sep 26 2012 16:51:11 (1348703471)
< Content-Type: application/json
< Date: Fri, 05 Oct 2012 15:20:26 GMT
< Pragma: no-cache
< Expires: Fri, 01 Jan 1990 00:00:00 GMT
< Cache-Control: no-cache, no-store, must-revalidate
< Content-Length: 171
* HTTP error before end of send, stop sending
< 
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}
* Closing connection #0

你知道正确的翻译吗?

出于安全原因,我删除了代码示例的访问令牌

4

1 回答 1

2

刚刚找到解决方案

数据值必须是这样的:

data = [
    ('metadata', (
        c.FORM_FILE, 'metadata.txt', 
        c.FORM_CONTENTTYPE, 'application/json')),
    ('file', (
        c.FORM_FILE, 'upload.jpg', 
        c.FORM_CONTENTTYPE, 'image/jpeg'))
]
于 2012-10-05T15:43:35.877 回答