Skip to content

Instantly share code, notes, and snippets.

Embed
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. https://api.twitter.com/oauth/authorize?oauth_token=XXXXXXX)
# this method automatically grabs the request token first
# Note: must ensure a callback URL (can be any URL) is defined for the application at dev.twitter.com,
# otherwise this will fail (401 Unauthorized)
try:
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