def authorize(request): """Get an unauth token and redirect user to Twitter's authorization URL.""" next = request.GET.get('next', None) if next: request.session['TWITTER_NEXT_URL'] = next try: tw = TwitterAPI() try: token = tw.get_unauthorised_request_token() except KeyError: # retry once tw.close() tw = TwitterAPI() token = tw.get_unauthorised_request_token() request.session['TWITTER_UNAUTH_TOKEN'] = token.to_string() auth_url = tw.get_authorisation_url(token) response = HttpResponseRedirect(auth_url) tw.close() return response except Exception, e: _log.exception(e) if request.user.is_authenticated(): add_message(request, u"We are unable to connect to Twitter at the moment. Please try again in a few minutes.") else: add_message(request, u"We are unable to connect to Twitter at the moment. Please continue signing up below. You will be able to connect your account to Twitter later from our homepage.") return _denied_response(request)