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('http://feedparser.org/docs/examples/digest_auth.xml')
>>> d.status
401
>>> d.headers['www-authenticate']
'Basic realm="Use test/basic"'
>>> d = feedparser.parse('http://feedparser.org/docs/examples/digest_auth.xml')
>>> d.status
401
>>> d.headers['www-authenticate']
'Digest realm="DigestTest",
 nonce="+LV/uLLdAwA=5d77397291261b9ef256b034e19bcb94f5b7992a",
 algorithm=MD5,
 qop="auth"'

As you see from the example above, the authentication method is stated in the response headers.
For more information on Digest Authentication look here (http://en.wikipedia.org/wiki/Digest_access_authentication).

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 (http://docs.python.org/library/urllib2.html) 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',
                          uri='https://mahler:8092/site-updates.py',
                          user='klem',
                          passwd='kadidd!ehopper')
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('http://feedparser.org/docs/examples/basic_auth.xml')
>>> d.status
401
>>> d.headers['www-authenticate']
'Basic realm="BasicTest"'
>>> realm = re.findall('realm="([^"]*)"', d.headers['www-authenticate'])[0]
>>> realm
'BasicTest'

I applied the suggested changes.