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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
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 ?
#
No idea. I actually lifted that straight from django_openidconsumer.views and it worked ... so I kept it. :)
#
Err, on second look, that was probably a paste error. Edited. Thanks!
#
get_contact_emails can be implemented way more efficiently by getting all contacts in one API call:
#
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.