2012-10-05 54 views
1

我掙扎使用pycurl來翻譯這個命令(即我可以成功運行)翻譯curl命令來PyCurl

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

到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 

你知道正確的翻譯嗎?

我已經刪除的代碼示例訪問令牌出於安全原因

+0

的文檔是在這裏:HTTPS: //developers.google.com/drive/manage-uploads#multipart –

回答

2

只是找到了解決辦法

數據值必須是這樣的:

data = [ 
    ('metadata', (
     c.FORM_FILE, 'metadata.txt', 
     c.FORM_CONTENTTYPE, 'application/json')), 
    ('file', (
     c.FORM_FILE, 'upload.jpg', 
     c.FORM_CONTENTTYPE, 'image/jpeg')) 
]