0

I was having a problem accessing my Gmail Atom feeds using feedparser module. For a non-password protected fees like a blog, for example,

import feedparser

d = feedparser.parse('http://karanjthakkar.wordpress.com/feed/')
print d.feed.title

The values that the feedparser module returned were correct. However when I used it using this to access my Gmail feed,

import urllib2, feedparser

def main():
 pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
 pwdmgr.add_password("New mail feed", 'http://mail.google.com/', "karanjthakkar", "my-password")
 auth = urllib2.HTTPBasicAuthHandler(pwdmgr)
 opener = urllib2.build_opener(auth)
 data = opener.open('http://mail.google.com/mail/feed/atom')
 d = feedparser.parse(data)
 print d

if __name__ == '__main__'
 main()

I got an Error 401 in the feed that was captured. This is what was captured:

screenshot

Am I missing something? I am not from a CS background so whatever I know is what I've read around. I intend to use the Gmail feeds captured to check the number of unread messages and display them using an Arduino.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Karan Thakkar
  • 414
  • 2
  • 9
  • 26

1 Answers1

3

I had no luck with HTTPDigestAuthHandler, but was able to get it working with HTTPBasicAuthHandler.

import urllib2, feedparser

pwdmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwdmgr.add_password("New mail feed", 'http://mail.google.com/', username, password)
auth = urllib2.HTTPBasicAuthHandler(pwdmgr)
opener = urllib2.build_opener(auth)
data = opener.open('http://mail.google.com/mail/feed/atom')
d = feedparser.parse(data)
print d
Yunchi
  • 5,529
  • 2
  • 17
  • 18
  • Do I enter the username and password directly or in single or double quotes? What I mean is which out of these three initializations do I use: 1. pwdmgr.add_password("New mail feed", 'http://mail.google.com/', 'username', 'password') 2. pwdmgr.add_password("New mail feed", 'http://mail.google.com/', 'username', "password") 3. pwdmgr.add_password("New mail feed", 'http://mail.google.com/', username, password) When I use the 3., I get "Invalid syntax" for that line. When I use 1. and 2., I get a "HTTP Error 401 : Basic Auth Failed" – Karan Thakkar Jun 22 '12 at 09:19
  • Either 1 or 2 will work. There's no difference between single quotes and double quotes in Python. 3 assumes you have variables named username and password. I think what you are doing wrong is that you should use "http://mail.google.com/" instead of "mail.google.com". The documentation specifies that the argument should be a URI. "mail.google.com" is a domain name. Adding the URI specifier "http://" makes it a valid URI. – Yunchi Jun 22 '12 at 12:54
  • Actually I just realized that it was Stackoverflow removing the http:// and making it into a link. Try specifying the exact URI to the feed in PasswordMgr instead of the top-level URI. Make sure you are using HTTPBasicAuthHandler, and that you are using the handler you made to open the feed URI rather than feedparser. – Yunchi Jun 22 '12 at 13:05
  • okay, @Woody. So what I did was, take the exact code you provided and changed the line 4 in your code to: pwdmgr.add_password("New mail feed", 'https://mail.google.com/', "karanjthakkar", "my-password") another change was changing the "http" to "https" in line 4 and line 7. But it still did not work. I get 401 yet again. Basic auth failed. – Karan Thakkar Jun 22 '12 at 13:11
  • AFAIK the code I provided should work, and it's working for my gmail accounts. Maybe edit your question so that I can see exactly what you have now. There's also another similar question that takes the same approach http://stackoverflow.com/a/1777142/1448071 – Yunchi Jun 22 '12 at 13:30
  • That code works for me. I don't have any more ideas. Check your password and see if you have some security setting turned on that might prevent you from accessing the feed? – Yunchi Jun 23 '12 at 01:54
  • I changed the code and added it into a function. The whole thing as you can see in the code that I modified in the question. It works great now. – Karan Thakkar Jun 23 '12 at 23:44
  • Not exactly sure what you changed to make it work, but glad it works now :) – Yunchi Jun 24 '12 at 18:08