Comment 6 for bug 746803

intelx86 (intelx) wrote :

HTTPDigestAuthHandler could not be used as Gmail feed does not use digest authentication.
Digest authentication constists of the realm's (typically a description of the computer or system being accessed) and the password's MD5 (or other algorithm) digest. To check if a password protected page uses Basic Auth or Digest Auth simply:

>>> import feedparser
>>> d = feedparser.parse('')
>>> d.status
>>> d.headers['www-authenticate']
'Basic realm="Use test/basic"'
>>> d = feedparser.parse('')
>>> d.status
>>> d.headers['www-authenticate']
'Digest realm="DigestTest",

As you see from the example above, the authentication method is stated in the response headers.
For more information on Digest Authentication look here (

I don't use HTTPBasicAuthHandler because it requires the specific realm ('New mail feed' for Gmail) otherwise it won't get authorized.
The example shown at urllib2 manual ( needs the specific realm or it fails.

import urllib2
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
opener = urllib2.build_opener(auth_handler)

If we use that method we need first to acquire the right realm and then proceed to the login.
>>> import feedparser, re
>>> d = feedparser.parse('')
>>> d.status
>>> d.headers['www-authenticate']
'Basic realm="BasicTest"'
>>> realm = re.findall('realm="([^"]*)"', d.headers['www-authenticate'])[0]
>>> realm

I applied the suggested changes.