I've tried a variety of ways to send "raw multipart data" to twitter's
update_background_image REST api.
specs:
http://apiwiki.twitter.com/REST+API+Documentation#updateprofilebackgroundimage
The problem is that I get back a server error. I'd appreciate any
help. I'm probably misunderstanding "raw multipart data", so feel free
to offer resources that might help.
Currently I'm using python with the urllib and urllib2 standard
libraries. I looked at pycurl but that would be a pain to integrate
because everyone on the project uses different platforms (for which
pycurl doesn't provide binaries) and possess varying levels of
'install-fu'.
I found this to be a useful tutorial on urllib and urllib2:
http://www.voidspace.org.uk/python/articles/urllib2.shtml
I've tried sending the image opened, eg "open(imgname, 'rb'), and
mime'd, according to this resource:
http://www.python.org/doc/2.2.3/lib/node397.html
Here is some python code and errors. Be sure to provide a twitter
username and password instead of XXX.
>>> username = XXX
>>> password = XXX
>>> url = 'http://twitter.com/account/update_profile_background_image.json'
>>> imgname = 'media/tcal/img/twitter_icon.jpg'
>>> fp = open(imgname, 'rb')
>>> img = MIMEImage(fp.read())
>>> fp.close()
>>> values = {'image':img }
>>> data = urllib.urlencode(values)
>>> auth = urllib2.HTTPPasswordMgrWithDefaultRealm()
>>> auth.add_password(None, 'http://twitter.com/account/', username, password)
>>> authHandler = urllib2.HTTPBasicAuthHandler(authHandler)
>>> opener = urllib2.build_opener(authHandler)
>>> opener.open(url, data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 387, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 419, in error
result = self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 823, in http_error_401
url, req, headers)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 801, in http_error_auth_reqed
return self.retry_http_basic_auth(host, req, realm)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 811, in retry_http_basic_auth
return self.parent.open(req)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 387, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 498, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 425, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/urllib2.py", line 506, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
>>> try:
... opener.open(url, data)
... except IOError, e:
... print e.msg
... print e.message
... print e.headers
... print e.info
... print e.code
... print e.filename
... print e.hdrs
...
Internal Server Error
Date: Fri, 20 Feb 2009 23:33:23 GMT
Server: hi
Last-Modified: Fri, 20 Feb 2009 23:33:23 GMT
Status: 500 Internal Server Error
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-
check=0
Content-Type: text/html; charset=utf-8
Content-Length: 4491
Expires: Tue, 31 Mar 1981 05:00:00 GMT
X-Revision: f6f5cf9a6a6126d6bcbde0d04f0484fa2e41de04
X-Transaction: 1235172803-94142-20328
Set-Cookie: lang=en; path=/
Set-Cookie: lang=en; path=/
Set-Cookie:
_twitter_sess=BAh7CToTcGFzc3dvcmRfdG9rZW4iLWI4NzlhMDY4NzExNjU3Mzg4NzgzYTJi
%250ANmNhYzQ0ODJhZjFjN2ExYzg6CXVzZXJpBJVMGAE6B2lkIiU3OTIyMTIyYzIy
%250AZWZjYTJiNzU2MDAzNzNhNjFiN2M0NiIKZmxhc2hJQzonQWN0aW9uQ29udHJv
%250AbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--
f3377a9ca3b85f32ccc0d06a6293d95cbc16245d; domain=.twitter.com; path=/
Vary: Accept-Encoding
Connection: close
<bound method HTTPError.info of HTTPError()>
500
http://twitter.com/account/update_profile_background_image.json
Date: Fri, 20 Feb 2009 23:33:23 GMT
Server: hi
Last-Modified: Fri, 20 Feb 2009 23:33:23 GMT
Status: 500 Internal Server Error
Pragma: no-cache
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-
check=0
Content-Type: text/html; charset=utf-8
Content-Length: 4491
Expires: Tue, 31 Mar 1981 05:00:00 GMT
X-Revision: f6f5cf9a6a6126d6bcbde0d04f0484fa2e41de04
X-Transaction: 1235172803-94142-20328
Set-Cookie: lang=en; path=/
Set-Cookie: lang=en; path=/
Set-Cookie:
_twitter_sess=BAh7CToTcGFzc3dvcmRfdG9rZW4iLWI4NzlhMDY4NzExNjU3Mzg4NzgzYTJi
%250ANmNhYzQ0ODJhZjFjN2ExYzg6CXVzZXJpBJVMGAE6B2lkIiU3OTIyMTIyYzIy
%250AZWZjYTJiNzU2MDAzNzNhNjFiN2M0NiIKZmxhc2hJQzonQWN0aW9uQ29udHJv
%250AbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA%253D%253D--
f3377a9ca3b85f32ccc0d06a6293d95cbc16245d; domain=.twitter.com; path=/
Vary: Accept-Encoding
Connection: close
thanks!
lucy.