Login

Google Contacts API friend finder

Author:
dgouldin
Posted:
April 15, 2008
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

This view will provide a link for AuthSub proxy authentication to the Google Contacts API and then grab the user's Google contacts and compare against User.email for possible user relationships.

A couple of things to note:

  • Dependency on the GData Python client library: http://code.google.com/p/gdata-python-client/

  • The domain hosting the view must be verified by Google's domain manager (otherwise API calls will result in a 403): https://www.google.com/accounts/ManageDomains

Thanks to Simon for get_url_host and get_full_url.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from django.contrib.auth.models import User
from django.http import get_host
from django.shortcuts import render_to_response as render
from django.utils.html import escape
import gdata.contacts.service

GOOGLE_CONTACTS_URI = 'http://www.google.com/m8/feeds/'

def get_url_host(request):
    if request.is_secure():
        protocol = 'https'
    else:
        protocol = 'http'
    host = escape(get_host(request))
    return '%s://%s' % (protocol, host)

def get_full_url(request):
    return get_url_host(request) + request.get_full_path()

def get_auth_sub_url(next):
    scope = GOOGLE_CONTACTS_URI
    secure = False
    session = True
    contacts_service = gdata.contacts.service.ContactsService()
    return contacts_service.GenerateAuthSubURL(next, scope, secure, session);

def get_contact_emails(authsub_token):
    contacts_service = gdata.contacts.service.ContactsService()
    contacts_service.auth_token = authsub_token
    contacts_service.UpgradeToSessionToken()
    emails = []
    feed = contacts_service.GetContactsFeed()
    emails.extend(sum([[email.address for email in entry.email] for entry in feed.entry], []))
    next_link = feed.GetNextLink()
    while next_link:
        feed = contacts_service.GetContactsFeed(uri=next_link.href)
        emails.extend(sum([[email.address for email in entry.email] for entry in feed.entry], []))
        next_link = feed.GetNextLink()
    return emails

def import_contacts(request):
    if request.GET.get('token', ''):
        emails = get_contact_emails(request.GET['token'])
        users = User.objects.filter(email__in=emails)
        return render('google_contacts/results.html', {
            'users': users
        })
    else:
        next = get_full_url(request)
        return render('google_contacts/login.html', {
        'auth_sub_url': get_auth_sub_url(next)
        })

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

zyegfryed (on April 16, 2008):

Hi, I don't understand why the variables host and protocol are computed inside get_ful_url(), because they are not used. Is there any reason ?

#

dgouldin (on April 16, 2008):

No idea. I actually lifted that straight from django_openidconsumer.views and it worked ... so I kept it. :)

#

dgouldin (on April 16, 2008):

Err, on second look, that was probably a paste error. Edited. Thanks!

#

timmolendijk (on November 12, 2009):

get_contact_emails can be implemented way more efficiently by getting all contacts in one API call:

query = ContactsQuery(params = {'max-results': '1000000'})
feed = contacts_service.GetContactsFeed(query.ToUri())

#

zenx (on January 21, 2010):

I had to replace contacts_service.auth_token = authsub_token with contacts_service.SetAuthSubToken(authsub_token) in order to make it work

#

Please login first before commenting.