使用python从gmail检索所有联系人 [英] Retrieve all contacts from gmail using python

查看:136
本文介绍了使用python从gmail检索所有联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django社交认证来检索Gmail中的联系人。获取授权没有任何问题。我做一个请求,然后我使用lxml来检索电子邮件地址。

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.

问题是它不显示每个联系人。例如,我可以检索30个联系人,而我的Gmail帐户有300多个联系人。

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.

这是我的观点:

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 ?

非常感谢您的帮助! p>

Thank you very much for your help !

推荐答案

由于联系人API文档说


Contacts API对于结果它可以一次返回,即使您明确请求所有可能的结果。如果请求的Feed具有比在单个响应中可以返回的字段更多的字段,API将截断Feed,并添加一个下一步链接,允许您请求其余响应。

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).

如果您不想进行额外的解析,可以尝试使用 start-index 参数请求额外的联系人(即,您的程序已经检索到30个,所以您将为下一个查询设置 start-index 为31。该部分还建议您可以覆盖返回结果的限制:

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:


如果您想收到所有联系人,而不是默认的最大值,您可以为 max-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.

这篇关于使用python从gmail检索所有联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆