Add oauth_verifier #4

Merged
avsd merged 4 commits from master into master 2013-05-03 14:29:14 -07:00
2 changed files with 18 additions and 15 deletions

View file

@ -1,11 +1,10 @@
#!/usr/bin/python #!/usr/bin/python
import sys, os
from setuptools import setup from setuptools import setup
from setuptools import find_packages from setuptools import find_packages
__author__ = 'Ryan McGrath <ryan@venodesigns.net>' __author__ = 'Ryan McGrath <ryan@venodesigns.net>'
__version__ = '1.4.5' __version__ = '1.4.6'
setup( setup(
# Basic package information. # Basic package information.
@ -17,7 +16,11 @@ setup(
include_package_data = True, include_package_data = True,
# Package dependencies. # Package dependencies.
install_requires = ['simplejson', 'oauth2', 'httplib2', 'twython', 'django'], install_requires = ['simplejson',
'oauth2',
'httplib2',
'twython>=2.7.2',
'django'],
provides = ['twython_django_oauth'], provides = ['twython_django_oauth'],
# Metadata for PyPI. # Metadata for PyPI.
@ -36,4 +39,4 @@ setup(
'Topic :: Communications :: Chat', 'Topic :: Communications :: Chat',
'Topic :: Internet' 'Topic :: Internet'
] ]
) )

View file

@ -1,8 +1,7 @@
from django.contrib.auth import authenticate, login, logout as django_logout from django.contrib.auth import authenticate, login, logout as django_logout
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
@ -12,9 +11,10 @@ from twython import Twython
# about adapting this to your own setup. # about adapting this to your own setup.
from twython_django_oauth.models import TwitterProfile from twython_django_oauth.models import TwitterProfile
def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL): def logout(request, redirect_url=settings.LOGOUT_REDIRECT_URL):
""" """
Nothing hilariously hidden here, logs a user out. Strip this out if your Nothing hilariously hidden here, logs a user out. Strip this out if your
application already has hooks to handle this. application already has hooks to handle this.
""" """
django_logout(request) django_logout(request)
@ -31,10 +31,10 @@ def begin_auth(request):
twitter_secret = settings.TWITTER_SECRET, twitter_secret = settings.TWITTER_SECRET,
callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks')) callback_url = request.build_absolute_uri(reverse('twython_django_oauth.views.thanks'))
) )
# Request an authorization url to send the user to... # Request an authorization url to send the user to...
auth_props = twitter.get_authentication_tokens() auth_props = twitter.get_authentication_tokens()
# Then send them over there, durh. # Then send them over there, durh.
request.session['request_token'] = auth_props request.session['request_token'] = auth_props
return HttpResponseRedirect(auth_props['auth_url']) return HttpResponseRedirect(auth_props['auth_url'])
@ -42,8 +42,8 @@ def begin_auth(request):
def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL): def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
""" """
A user gets redirected here after hitting Twitter and authorizing your A user gets redirected here after hitting Twitter and authorizing your
app to use their data. app to use their data.
*** ***
This is the view that stores the tokens you want This is the view that stores the tokens you want
for querying data. Pay attention to this. for querying data. Pay attention to this.
@ -57,10 +57,10 @@ def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
oauth_token = request.session['request_token']['oauth_token'], oauth_token = request.session['request_token']['oauth_token'],
oauth_token_secret = request.session['request_token']['oauth_token_secret'], oauth_token_secret = request.session['request_token']['oauth_token_secret'],
) )
# Retrieve the tokens we want... # Retrieve the tokens we want...
authorized_tokens = twitter.get_authorized_tokens() authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
# If they already exist, grab them, login and redirect to a page displaying stuff. # If they already exist, grab them, login and redirect to a page displaying stuff.
try: try:
user = User.objects.get(username = authorized_tokens['screen_name']) user = User.objects.get(username = authorized_tokens['screen_name'])
@ -72,7 +72,7 @@ def thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):
profile.oauth_token = authorized_tokens['oauth_token'] profile.oauth_token = authorized_tokens['oauth_token']
profile.oauth_secret = authorized_tokens['oauth_token_secret'] profile.oauth_secret = authorized_tokens['oauth_token_secret']
profile.save() profile.save()
user = authenticate( user = authenticate(
username = authorized_tokens['screen_name'], username = authorized_tokens['screen_name'],
password = authorized_tokens['oauth_token_secret'] password = authorized_tokens['oauth_token_secret']