Google GData Authentication decorator

Check out the new site at https://rkblog.dev.

Google GData API can be used to retreive data from Google services... It can also be implemented on other sites (which I have on my ToDo list at my current job) helping in creation of backed applications. "Read" to see a simple view with custom decorator that authenticate user agains a GData provider (Google in this case). You can also read the auth docs.

#!/usr/bin/python
from httplib import HTTPSConnection, HTTPConnection
from BeautifulSoup import BeautifulStoneSoup

from django.shortcuts import render_to_response
from django.conf import settings
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse

def gdata_required(f):
	"""
	Authenticate against Google GData service
	"""
	def wrap(request, *args, **kwargs):
		if 'token' not in request.GET and 'token' not in request.session:
			# no token at all, request one-time-token
			# next: where to redirect
			# scope: what service you want to get access to
			return HttpResponseRedirect("https://www.google.com/accounts/AuthSubRequest?next=http://YOUR_URL.com/&scope=http://docs.google.com/feeds/documents&session=1")
		elif 'token' not in request.session and 'token' in request.GET:
			# request session token using one-time-token
			conn = HTTPSConnection("www.google.com")
			conn.putrequest('GET', '/accounts/AuthSubSessionToken')
			conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET['token'])
			conn.endheaders()
			conn.send(' ')
			r = conn.getresponse()
			if str(r.status) == '200':
				token = r.read()
				token = token.split('=')[1]
				token = token.replace('
', '')
				request.session['token'] = token
		return f(request, *args, **kwargs)
	wrap.__doc__=f.__doc__
	wrap.__name__=f.__name__
	return wrap

@gdata_required
def index(request):
	"""
	Simple example - list google docs documents
	"""
	if 'token' in request.session:
		con = HTTPConnection("docs.google.com")
		con.putrequest('GET', '/feeds/documents/private/full/')
		con.putheader('Authorization', 'AuthSub token="%s"' % request.session['token'])
		con.endheaders()
		con.send('')
		r2 = con.getresponse()
		dane = r2.read()
		soup = BeautifulStoneSoup(dane)
		dane = soup.prettify()
		return render_to_response('a.html', {'dane':dane}, context_instance=RequestContext(request))
	else:
		return render_to_response('a.html', {'dane':'bad bad'}, context_instance=RequestContext(request))

@gdata_required
def query(request):
	"""
	Search in documents
	"""
	if 'token' in request.session:
		if 'q' in request.GET:
			q = request.GET['q']
		else:
			q=''
		con = HTTPConnection("docs.google.com")
		con.putrequest('GET', '/feeds/documents/private/full/?q=%s' % q)
		con.putheader('Authorization', 'AuthSub token="%s"' % request.session['token'])
		con.endheaders()
		con.send('')
		r2 = con.getresponse()
		dane = r2.read()
		soup = BeautifulStoneSoup(dane)
		dane = soup.prettify()
		return render_to_response('b.html', {'dane':dane}, context_instance=RequestContext(request))
	else:
		return render_to_response('b.html', {'dane':'bad bad'}, context_instance=RequestContext(request))
RkBlog

Django web framework tutorials, 14 July 2008


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact