Fixed to work with Twython 3.0.0 or greater and cleanup
Added LICENSE updated README updated setup.py PEP8'd stuff
This commit is contained in:
parent
445f9bde8b
commit
30a57c7950
8 changed files with 181 additions and 205 deletions
|
|
@ -1,12 +1,13 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class TwitterProfile(models.Model):
|
||||
"""
|
||||
An example Profile model that handles storing the oauth_token and
|
||||
oauth_secret in relation to a user. Adapt this if you have a current
|
||||
setup, there's really nothing special going on here.
|
||||
"""
|
||||
user = models.OneToOneField(User)
|
||||
oauth_token = models.CharField(max_length = 200)
|
||||
oauth_secret = models.CharField(max_length = 200)
|
||||
"""
|
||||
An example Profile model that handles storing the oauth_token and
|
||||
oauth_secret in relation to a user. Adapt this if you have a current
|
||||
setup, there's really nothing special going on here.
|
||||
"""
|
||||
user = models.OneToOneField(User)
|
||||
oauth_token = models.CharField(max_length=200)
|
||||
oauth_secret = models.CharField(max_length=200)
|
||||
|
|
|
|||
|
|
@ -2,18 +2,17 @@ from django.conf.urls.defaults import *
|
|||
|
||||
# your_app = name of your root djang app.
|
||||
urlpatterns = patterns('twython_django_oauth.views',
|
||||
|
||||
# First leg of the authentication journey...
|
||||
url(r'^login/?$', "begin_auth", name="twitter_login"),
|
||||
# First leg of the authentication journey...
|
||||
url(r'^login/?$', "begin_auth", name="twitter_login"),
|
||||
|
||||
# Logout, if need be
|
||||
url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not
|
||||
|
||||
# This is where they're redirected to after authorizing - we'll
|
||||
# further (silently) redirect them again here after storing tokens and such.
|
||||
url(r'^thanks/?$', "thanks", name="twitter_callback"),
|
||||
# Logout, if need be
|
||||
url(r'^logout/?$', "logout", name="twitter_logout"), # Calling logout and what not
|
||||
|
||||
# An example view using a Twython method with proper OAuth credentials. Clone
|
||||
# this view and url definition to get the rest of your desired pages/functionality.
|
||||
url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"),
|
||||
# This is where they're redirected to after authorizing - we'll
|
||||
# further (silently) redirect them again here after storing tokens and such.
|
||||
url(r'^thanks/?$', "thanks", name="twitter_callback"),
|
||||
|
||||
# An example view using a Twython method with proper OAuth credentials. Clone
|
||||
# this view and url definition to get the rest of your desired pages/functionality.
|
||||
url(r'^user_timeline/?$', "user_timeline", name="twitter_timeline"),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -13,84 +13,72 @@ from twython_django_oauth.models import TwitterProfile
|
|||
|
||||
|
||||
def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
|
||||
"""
|
||||
Nothing hilariously hidden here, logs a user out. Strip this out if your
|
||||
application already has hooks to handle this.
|
||||
"""
|
||||
django_logout(request)
|
||||
return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
|
||||
"""
|
||||
Nothing hilariously hidden here, logs a user out. Strip this out if your
|
||||
application already has hooks to handle this.
|
||||
"""
|
||||
django_logout(request)
|
||||
return HttpResponseRedirect(request.build_absolute_uri(redirect_url))
|
||||
|
||||
|
||||
def begin_auth(request):
|
||||
"""
|
||||
The view function that initiates the entire handshake.
|
||||
For the most part, this is 100% drag and drop.
|
||||
"""
|
||||
# Instantiate Twython with the first leg of our trip.
|
||||
twitter = Twython(
|
||||
twitter_token = settings.TWITTER_KEY,
|
||||
twitter_secret = settings.TWITTER_SECRET,
|
||||
callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks'))
|
||||
)
|
||||
"""The view function that initiates the entire handshake.
|
||||
|
||||
# Request an authorization url to send the user to...
|
||||
auth_props = twitter.get_authentication_tokens()
|
||||
For the most part, this is 100% drag and drop.
|
||||
"""
|
||||
# Instantiate Twython with the first leg of our trip.
|
||||
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET)
|
||||
|
||||
# Request an authorization url to send the user to...
|
||||
callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks'))
|
||||
auth_props = twitter.get_authentication_tokens(callback_url)
|
||||
|
||||
# Then send them over there, durh.
|
||||
request.session['request_token'] = auth_props
|
||||
return HttpResponseRedirect(auth_props['auth_url'])
|
||||
|
||||
# Then send them over there, durh.
|
||||
request.session['request_token'] = auth_props
|
||||
return HttpResponseRedirect(auth_props['auth_url'])
|
||||
|
||||
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
|
||||
"""
|
||||
A user gets redirected here after hitting Twitter and authorizing your
|
||||
app to use their data.
|
||||
"""A user gets redirected here after hitting Twitter and authorizing your app to use their data.
|
||||
|
||||
***
|
||||
This is the view that stores the tokens you want
|
||||
for querying data. Pay attention to this.
|
||||
***
|
||||
"""
|
||||
# Now that we've got the magic tokens back from Twitter, we need to exchange
|
||||
# for permanent ones and store them...
|
||||
twitter = Twython(
|
||||
twitter_token = settings.TWITTER_KEY,
|
||||
twitter_secret = settings.TWITTER_SECRET,
|
||||
oauth_token = request.session['request_token']['oauth_token'],
|
||||
oauth_token_secret = request.session['request_token']['oauth_token_secret'],
|
||||
)
|
||||
This is the view that stores the tokens you want
|
||||
for querying data. Pay attention to this.
|
||||
|
||||
# Retrieve the tokens we want...
|
||||
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
|
||||
"""
|
||||
# Now that we've got the magic tokens back from Twitter, we need to exchange
|
||||
# for permanent ones and store them...
|
||||
oauth_token = request.session['request_token']['oauth_token']
|
||||
oauth_token_secret = request.session['request_token']['oauth_token_secret']
|
||||
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
|
||||
oauth_token, oauth_token_secret)
|
||||
|
||||
# If they already exist, grab them, login and redirect to a page displaying stuff.
|
||||
try:
|
||||
user = User.objects.get(username = authorized_tokens['screen_name'])
|
||||
except User.DoesNotExist:
|
||||
# We mock a creation here; no email, password is just the token, etc.
|
||||
user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
|
||||
profile = TwitterProfile()
|
||||
profile.user = user
|
||||
profile.oauth_token = authorized_tokens['oauth_token']
|
||||
profile.oauth_secret = authorized_tokens['oauth_token_secret']
|
||||
profile.save()
|
||||
# Retrieve the tokens we want...
|
||||
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
|
||||
|
||||
# If they already exist, grab them, login and redirect to a page displaying stuff.
|
||||
try:
|
||||
user = User.objects.get(username=authorized_tokens['screen_name'])
|
||||
except User.DoesNotExist:
|
||||
# We mock a creation here; no email, password is just the token, etc.
|
||||
user = User.objects.create_user(authorized_tokens['screen_name'], "fjdsfn@jfndjfn.com", authorized_tokens['oauth_token_secret'])
|
||||
profile = TwitterProfile()
|
||||
profile.user = user
|
||||
profile.oauth_token = authorized_tokens['oauth_token']
|
||||
profile.oauth_secret = authorized_tokens['oauth_token_secret']
|
||||
profile.save()
|
||||
|
||||
user = authenticate(
|
||||
username=authorized_tokens['screen_name'],
|
||||
password=authorized_tokens['oauth_token_secret']
|
||||
)
|
||||
login(request, user)
|
||||
return HttpResponseRedirect(redirect_url)
|
||||
|
||||
user = authenticate(
|
||||
username = authorized_tokens['screen_name'],
|
||||
password = authorized_tokens['oauth_token_secret']
|
||||
)
|
||||
login(request, user)
|
||||
return HttpResponseRedirect(redirect_url)
|
||||
|
||||
def user_timeline(request):
|
||||
"""
|
||||
An example view with Twython/OAuth hooks/calls to fetch data about the user
|
||||
in question. Pretty self explanatory if you read through it...
|
||||
"""
|
||||
user = request.user.twitterprofile
|
||||
twitter = Twython(
|
||||
twitter_token = settings.TWITTER_KEY,
|
||||
twitter_secret = settings.TWITTER_SECRET,
|
||||
oauth_token = user.oauth_token,
|
||||
oauth_token_secret = user.oauth_secret
|
||||
)
|
||||
user_tweets = twitter.getHomeTimeline()
|
||||
return render_to_response('tweets.html', {'tweets': user_tweets})
|
||||
"""An example view with Twython/OAuth hooks/calls to fetch data about the user in question."""
|
||||
user = request.user.twitterprofile
|
||||
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET,
|
||||
user.oauth_token, user.oauth_secret)
|
||||
user_tweets = twitter.get_home_timeline()
|
||||
return render_to_response('tweets.html', {'tweets': user_tweets})
|
||||
|
|
|
|||
Reference in a new issue