python查找联系人,使用python从gmail检索所有联系人

在使用Django Social Auth从Gmail获取联系人时遇到限制问题,只能获取到30个联系人,而实际有超过300个。问题在于Google Contacts API会限制单次请求返回的结果数量。解决方案是需要进行分页处理,通过跟踪'Next'链接来获取剩余的联系人。可以尝试使用'start-index'参数请求更多联系人,或者检查是否能设置大数量的'max-results'来获取所有联系人。

I am using django social auth in order to retrieve contacts from gmail. I do not have any problem getting the authorization. I do a request and then I use lxml to retrieve the email addresses.

The problem is that it does not display every contacts. For example, I can retrieve only 30 contacts while I have more than 300 contacts with my gmail account.

Here is my view :

def get_email_google(request):

social = request.user.social_auth.get(provider='google-oauth2')

url = 'https://www.google.com/m8/feeds/contacts/default/full' + '?access_token=' + social.tokens['access_token']

req = urllib2.Request(url, headers={'User-Agent' : "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30"})

contacts = urllib2.urlopen(req).read()

contacts_xml = etree.fromstring(contacts)

contacts_list = []

for entry in contacts_xml.findall('{http://www.w3.org/2005/Atom}entry'):

for address in entry.findall('{http://schemas.google.com/g/2005}email'):

email = address.attrib.get('address')

contacts_list.append(email)

I can't figure out why I do not have every contact with that url.

Any idea on how I can get every contacts ?

Thank you very much for your help !

解决方案

The Contacts API has a hard limit to the number of results it can return at a time even if you explicitly request all possible results. If the requested feed has more fields than can be returned in a single response, the API truncates the feed and adds a "Next" link that allows you to request the rest of the response.

So you'll have to page through the contacts, following those "Next" links, until you have all the contacts (which you can detect by looking for a result without a 'Next' link).

If you don't want to do extra parsing, you could try using the start-index parameter to ask for extra contacts (ie. your program has retrieved 30, so you'll set start-index to 31 for the next query). That section also suggests you might be able to override the limit on returned results:

If you want to receive all of the contacts, rather than only the default maximum, you can specify a very large number for max-results.

But I wouldn't be surprised if that was false, and you'll have to use the paginated approach.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值