Created
March 11, 2009 21:10
-
-
Save idan/77742 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import django-related bits, including my own form code from elsewhere | |
from django.http import HttpResponseRedirect, HttpResponseServerError | |
from django.shortcuts import render_to_response | |
from contacts_manager.forms import * | |
# importing the gdata stuff | |
from gdata.contacts.service import ContactsService | |
from gdata.alt.appengine import run_on_appengine | |
from gdata.auth import generate_auth_sub_url, extract_auth_sub_token_from_url | |
# python utilities | |
import logging | |
import pickle | |
# I mapped the initial view to this | |
# ignore the form handling code. | |
# There's just a single textfield there which asks for the gapps domain | |
def login(request): | |
if request.method == 'POST': | |
form = DomainForm(request.POST) | |
if form.is_valid(): | |
# get the domain from the form which the user filled in | |
hd = form.cleaned_data['domain'] | |
if not hd: | |
hd = 'default' | |
# generate the auth_sub_url, tell it to redirect the browser back to my machine | |
# after the "user" has finished authentication | |
url = generate_auth_sub_url( | |
next='http://localhost:8080/authreturn/', | |
scopes=['http://www.google.com/m8/feeds/',], | |
domain=hd, | |
session=True) | |
# redirect the viewer to the generated url | |
return HttpResponseRedirect(url) | |
else: | |
form = DomainForm() | |
return render_to_response('login.html', {'form' : form,}) | |
# this is the view which handles browsers coming back from authentication | |
# here's where we get the single-use auth token and convert it to a session token | |
# then store the token in the session cookie by pickling it | |
def auth_return(request): | |
client = ContactsService() | |
run_on_appengine(client) | |
session_token = None | |
auth_token = extract_auth_sub_token_from_url(request.build_absolute_uri()) | |
if auth_token: | |
session_token = client.upgrade_to_session_token(auth_token) | |
request.session['session_token'] = pickle.dumps(session_token) | |
return HttpResponseRedirect('/contacts/') | |
# now we are in another view, and we need the session token to do anything | |
# get the token from the session, unpickle it, and it's ready to use. | |
def list_contacts(request): | |
session_token = pickle.loads(request.session['session_token']) | |
client = ContactsService() | |
run_on_appengine(client) | |
client.current_token = session_token | |
# get shit done... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment