如何使用Python自动登录到gmail atom feed? [英] How to auto log into gmail atom feed with Python?
问题描述
Gmail有这样一个甜蜜的东西来获得atom feed:
def gmail_url(user,pwd) $ b returnhttps://+ str(user)+:+ str(pwd)+@ gmail.google.com / gmail / feed / atom
现在,当您在浏览器中执行此操作时,它会对您进行身份验证并转发。但在Python中,至少我在尝试的是行不通的。
url = gmail_url(settings.USER, settings.PASS)
打印url
opener = urllib.FancyURLopener()
f = opener.open(url)
print f.read()
$ c
而不是正确转发,它是这样做的:
>>>
https://用户:pass@gmail.google.com/gmail/feed/atom
在mail.google.com上输入新邮件的用户名:
这是不好的!我不应该再次输入用户名和密码!我怎样才能使它在python中自动转发,就像它在我的web浏览器中一样,所以我可以得到没有所有BS的提要内容?
/ b>您可以使用HTTPBasicAuthHandler,我尝试了以下方法并使用它:
import urllib2
def get_unread_msgs(user,passwd):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm ='New mail feed',
uri =' https://mail.google.com',
user='%s@gmail.com'%user,
passwd = passwd
)
opener = urllib2.build_opener( auth_handler)
urllib2.install_opener(opener)
feed = urllib2.urlopen('https://mail.google.com/mail/feed/atom')
返回feed.read()
Gmail has this sweet thing going on to get an atom feed:
def gmail_url(user, pwd):
return "https://"+str(user)+":"+str(pwd)+"@gmail.google.com/gmail/feed/atom"
Now when you do this in a browser, it authenticates and forwards you. But in Python, at least what I'm trying, isn't working right.
url = gmail_url(settings.USER, settings.PASS)
print url
opener = urllib.FancyURLopener()
f = opener.open(url)
print f.read()
Instead of forwarding correctly, it's doing this:
>>>
https://user:pass@gmail.google.com/gmail/feed/atom
Enter username for New mail feed at mail.google.com:
This is BAD! I shouldn't have to type in the username and password again!! How can I make it just auto-forward in python as it does in my web browser, so I can get the feed contents without all the BS?
解决方案 You can use the HTTPBasicAuthHandler, I tried the following and it worked:
import urllib2
def get_unread_msgs(user, passwd):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm='New mail feed',
uri='https://mail.google.com',
user='%s@gmail.com' % user,
passwd=passwd
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
feed = urllib2.urlopen('https://mail.google.com/mail/feed/atom')
return feed.read()
这篇关于如何使用Python自动登录到gmail atom feed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!