Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import tweepy
from django.conf import settings
from django.core.urlresolvers import reverse
from django.core.context_processors import csrf
def get_authorization_url(request):
# URL to where we will redirect to
redirect_url = settings.SITE_URL + reverse('register_twitter')
# create the handler
auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY, settings.TWITTER_CONSUMER_SECRET, redirect_url)
# get the authorization url (i.e.
# this method automatically grabs the request token first
# Note: must ensure a callback URL (can be any URL) is defined for the application at,
# otherwise this will fail (401 Unauthorized)
url = auth.get_authorization_url()
except tweepy.TweepError:
# failed to get auth url (maybe twitter is down)
url = reverse('home')
return url
# store the returned values
request.session['twitter_request_token_key'] = auth.request_token.key
request.session['twitter_request_token_secret'] = auth.request_token.secret
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment