def add_token(request): #set the scope to contacts API cp_scope = gdata.service.lookup_scopes('cp') gd_client = gdata.base.service.GBaseService() gd_client.SetOAuthInputParameters( gdata.auth.OAuthSignatureMethod.RSA_SHA1, settings.GDATA_CREDS['key'], consumer_secret=settings.GDATA_CREDS['secret'], rsa_key=settings.GDATA_CREDS['rsa_key'] ) goog_url = '' if not (request.GET.has_key('oauth_token')): #we don't have a token yet #create a request token with the contacts api scope rt = gd_client.FetchOAuthRequestToken( scopes=cp_scope ) #store the token's secret in a session variable request.session['token_secret'] = rt.secret #get an authorization URL for our token from gdata gd_client.SetOAuthToken(rt) goog_url = gd_client.GenerateOAuthAuthorizationURL()\ +'&oauth_callback='+request.build_absolute_uri() params = { 'goog_url': goog_url, } return render_to_response('add_token.html', params, context_instance=RequestContext(request)) else: #we've been redirected back by google with the auth token as a query parameter #create a request token object from the URL (converts the query param into a token object) rt = gdata.auth.OAuthTokenFromUrl(url=request.build_absolute_uri()) #set the secret to what we saved above, before we went to Google rt.secret = request.session['token_secret'] #set the scope again rt.scopes = cp_scope; #upgrade our request token to an access token (where the money at) gd_client.UpgradeToOAuthAccessToken(authorized_request_token=rt) """this part is confusing: we have to retrieve the authorized access token by doing a lookup I have submitted an issue and a patch to the Python client to make UpgradeToOAuthAccessToken return the authorized token see: http://code.google.com/p/gdata-python-client/issues/detail?id=213""" at = gd_client.token_store.find_token(rt.scopes[0]) """save! how you store the data is arbitrary. I just pickle the token object, though you could probably store the token_key and token_secret individually and reconstruct the object later. (see views.home)""" ga = GoogleAccount() #my model ga.user = request.user ga.data = Pickle.dumps(at) ga.save() request.user.message_set.create(message="Your Google account has been "+ "successfully added.") return HttpResponseRedirect('/oauth')