Error with put_photo - UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 413: ordinal not in range(128) #33

Closed
sontek opened this Issue May 21, 2012 · 12 comments

3 participants

@sontek
(Pdb) image
<open file '/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/photos/uploads/upload_11.jpg', mode 'r' at 0x147f4b0>
(Pdb) c

Traceback (most recent call last):
File "/home/sontek/.virtualenvs/studiosnaps/local/lib/python2.7/site-packages/gevent/greenlet.py", line 390, in run
result = self._run(self.args, *self.kwargs)
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/controller.py", line 114, in upload_photo
self.uploader.upload_photo(self.view.get_upload_file())
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/upload.py", line 283, in upload_photo
photo_response = fp_graph.put_photo(photo, '', album_id)
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/facebook.py", line 175, in put_photo
object_id = album_id or "me"
File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
File "/usr/lib/python2.7/httplib.py", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 812, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 413: ordinal not in range(128)

I'm just calling:

            photo_response = graph_obj.put_photo(photo, '', album_id)
@sontek

req.data at line 190 has this fnky this_is_the_boundary stuff in it:

\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x07\xff\xd9\r\n------------ThIs_Is_tHe_bouNdaRY_$\r\nContent-Disposition: form-data; name="message"\r\n\r\nhi\r\n------------ThIs_Is_tHe_bouNdaRY_$--\r\n'

@martey

Do you still get this error if you open the file in binary mode? If so, would it be possible for you to provide me with the image that is causing the error?

@sontek

According to the docs binary mode only matters on windows and I'm on Linux. It also says it could corrupt JPEG files. I did try it but got the same exception.

Uploading the image does work with the facepy library that you can find here on github to compare how they are doing multipart forms.

Here is the failing photo:
http://studiosnaps.com/upload_26.jpg

@sontek

If it helps, this was uploading a photo to a page's album, not a personal album.

@martey

I tried to replicate this error using the image file you provided, but I have been unable to (the image uploaded successfully each time). Can you confirm that you are using the most recent version of this library?

@sontek

Yes, I was using the current version from master.

I'm using python 2.7.3 on Ubuntu LTS 12.04.

I was uploading to a fan/company page, not a personal profile page.

@martey

One of the computers I tested on is running python 2.7.3rc2. Since the error you are receiving is a UnicodeDecodeError, it has nothing to do with the type of Facebook profile you are trying to upload the photo to.

I tried more testing, but can still not replicate the error you are receiving. I noticed that your original traceback mentions gevent. Do you still get the error if you are not using it (i.e. using python on the command line)?

@martey

@sontek, I have not heard anything from you on this, and there have been no additional reports of people with the same issue, so I am going to close this.

@martey martey closed this Jul 16, 2012
@honi

I'm getting a UnicodeDecodeError while trying to upload a PIL generated PNG, stored in a django model.

If I try to upload another file, it works fine. So I think it must be related with how I'm generating the image or something with PIL. I know I'm not providing much information, but do you have any ideas?

@honi

Digging a bit more, I see that put_photo assumed the image is JPG: https://github.com/pythonforfacebook/facebook-sdk/blob/master/facebook.py#L261

Why is that? Facebook doesn't allow PNG?

@martey

Why is that? Facebook doesn't allow PNG?

Some documentation from the old REST API I found suggests that the following file types are supported:

*GIF *JPG *PNG *PSD *TIFF *JP2 *IFF *WBMP *XBM

This is a bug in the library, but should be created as a new issue (since it is not the same issue as was originally reported).

@honi

Created 2 issues. Posting here for reference:
UnicodeDecodeError: #43
Hardcoded JPG format: #44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment