def oauth_callback(request): if not xsrfutil.validate_token( settings.SECRET_KEY, request.REQUEST['state'], request.user): return HttpResponseBadRequest() credential = FLOW.step2_exchange(request.REQUEST) http = httplib2.Http() http = credential.authorize(http) if request.user.email: storage = Storage( CredentialsModel, 'id', request.user, 'credential') storage.locked_put(credential) return HttpResponseRedirect('/') else: user_info_service = build( serviceName='oauth2', version='v2', http=http) user_info = user_info_service.userinfo().get().execute() request.user.email = user_info['email'] request.user.first_name = user_info['name'] request.user.save() storage = Storage(CredentialsModel, 'id', request.user, 'credential') storage.locked_put(credential) return HttpResponseRedirect('/')