I have a python script that I cobbled together that checks my gmail via the rss feed and outputs the text to the screen. It worked on python 2.6.6 but I’ve been unsuccessful to get it working under python 3.2.3.
I’ve used 2to3 to convert it. The code is here:https://www.dropbox.com/s/b1277mi7vc7hv3f/gmailcheckparseconky.py3
The problem occurs in the ObtainEmailFeed function.
I get the dreaded “TypeError("expected bytes, not %s" % s.__class__.__name__)” error when I use
b64auth = base64.encodestring("%s:%s" % (user, password))
however when I use
string = bytes("%s:%s" % (user,password), 'utf-8')
string2=str(string,'utf-8')
b64auth = base64.encodestring( string)
I get
auth = "Basic " + b64auth
TypeError: Can't convert 'bytes' object to str implicitly
which seems to bring the whole problem back full circle.
I’ve tried hard coding in my password as text and that doesn’t work. I get an
urllib.error.HTTPError: HTTP Error 401: Unauthorized when I set `auth = “Basic user:password”
As I said I cobbled the code together. I don’t fully understand what gmail needs for me to authorize. Continuing along with that vein, I’d prefer help in fixing this code so I can learn and get a better understanding of python instead of pointing me to another script on the web that does the same/similar thing as to what I’m doing.
Thanks in advance,
Nick
Since python3 there is a distinction between bytes and strings. The error comes from the fact that
base64.encodestringis now a deprecated alias forencodebytes(see also: http://docs.python.org/py3k/library/base64.html#module-base64), and so it wants you to give bytes, and it will give you bytes back.You might want to do:
Then b64auth will be a string and you will be able to use it as string in rest of the code.