encoding problem with head first python programming

This is the place for queries that don't fit in any of the other categories.

encoding problem with head first python programming

Postby codefox » Sun Jun 09, 2013 10:32 am

Hi there,
I'm a complete noob so I hope I will give you the information in a good way so you are able to help me.

My OS: Ubuntu 12.10
My version of Python: 3.3.0

How do I encode the data my function is going to send to Twitter in the right way?

I'm using Head First Programming: Python to learn Python. In it (p. 98) is an exercise to have a program send messages to Twitter. Whereas the book demonstrates that it works, I get the error message that data I send cannot be of type string, but should be bytes or an iterable of bytes.

I think this may either be because the version of Python they use is 3.1.0 and it handles this kind of code differently, or because by now the programming of Twitter's website has changed. I googled the traceback error line but what came up was mainly a different example that contained

Traceback:
“Traceback (most recent call last):
File "/home/arjan/Python scripts/twitter message parameter 1.py", line 24, in <module>
send_to_twitter(get_price())
File "/home/arjan/Python scripts/twitter message parameter 1.py", line 11, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.3/urllib/request.py", line 471, in open
req = meth(req)
File "/usr/lib/python3.3/urllib/request.py", line 1183, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.”

Relevant part of definition of the function:

Code: Select all
 resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)


the code calling the function:

Code: Select all
 if price_now == "Y":
    send_to_twitter(get_price())


After searching on google I saw something on stackoverflow on encoding and I tried adding the line
Code: Select all
 params = params.encode('utf-8')
in front of the above line in the definition of the function, but then it gave a different traceback error.

in this case:
“Traceback (most recent call last):
File "/home/arjan/Python scripts/twitter message parameter 2.py", line 25, in <module>
send_to_twitter(get_price())
File "/home/arjan/Python scripts/twitter message parameter 2.py", line 12, in send_to_twitter
resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params)
File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.3/urllib/request.py", line 479, in open
response = meth(req, response)
File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.3/urllib/request.py", line 511, in error
result = self._call_chain(*args)
File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
result = func(*args)
File "/usr/lib/python3.3/urllib/request.py", line 696, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python3.3/urllib/request.py", line 479, in open
response = meth(req, response)
File "/usr/lib/python3.3/urllib/request.py", line 591, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python3.3/urllib/request.py", line 511, in error
result = self._call_chain(*args)
File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
result = func(*args)
File "/usr/lib/python3.3/urllib/request.py", line 969, in http_error_401
url, req, headers)
File "/usr/lib/python3.3/urllib/request.py", line 935, in http_error_auth_reqed
scheme)
ValueError: AbstractBasicAuthHandler does not support the following scheme: 'OAuth'”

Many thanks in advance for your help :-D
codefox
 
Posts: 1
Joined: Sun Jun 09, 2013 10:18 am

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], micseydel and 2 guests