4

I just updated to Twython 2.3.4, but now my Twitter auth stops working. It fails on the ' auth_props = twitter.get_authentication_tokens()' line. Any idea what went wrong? Thanks in advance!

The python code to do Twitter auth using Twython is below:

def begin_auth(request):
    twitter = Twython(
        twitter_token = TWITTER_KEY,
        twitter_secret = TWITTER_SECRET,
        callback_url = request.build_absolute_uri(reverse('portnoy.views.thanks'))
    )
    # Request an authorization url to send the user to...
    auth_props = twitter.get_authentication_tokens()

I have the following error on the line above: TwythonAuthError: "Seems something couldn't be verified with your OAuth junk. Error: 401, Message: Failed to validate oauth signature and token"

    # Then send them over there, durh.
    request.session['request_token'] = auth_props
    return HttpResponseRedirect(auth_props['auth_url'])

def thanks(request, redirect_url='/'):
    c = RequestContext(request)
    # for permanent ones and store them...
    twitter = Twython(
        twitter_token = TWITTER_KEY,
        twitter_secret = TWITTER_SECRET,
        oauth_token = request.session['request_token']['oauth_token'],
        oauth_token_secret = request.session['request_token']['oauth_token_secret']
     )

    # Retrieve the tokens we want...
    authorized_tokens = twitter.get_authorized_tokens()
    request.session['request_tokens'] = authorized_tokens
    debug('thanks', request.session['request_tokens'])

    user = User.objects.filter(username=authorized_tokens['screen_name'])
    if user.exists():
        user = user[0]
        user.backend='django.contrib.auth.backends.ModelBackend'     
        auth.login(request,user)
    else:
        return render_to_response('twitter_register.html', c)   
    return HttpResponseRedirect(redirect_url)
Arman
  • 1,074
  • 3
  • 20
  • 40

2 Answers2

2

I'm the author of Twython.

What version of Requests are you running under the hood? There was recently an issue where people kept running into various OAuth-related errors due to an upstream bug. Curious if it's related to that...

Ryan McGrath
  • 2,042
  • 14
  • 23
  • I'm running requests 0.13.9. pip install requests==0.13.9 Requirement already satisfied (use --upgrade to upgrade): requests==0.13.9 in /srv/my_project/venv/lib/python2.7/site-packages/requests-0.13.9-py2.7.egg Cleaning up... – Arman Sep 04 '12 at 08:13
  • 2
    It is marked as a solution, but I'm still confused. Was it the Requests version that was causing the error? – Kinetic Stack Jan 11 '13 at 01:29
  • I second @KineticStack question. Was it the Requests version that was causing the error? – Moonwalker Sep 17 '13 at 16:04
1

I ran into the same problem myself. It doesn't seem to work if you are testing locally.

Here's what I did to fix it:

  1. Edit your hosts file (on OSX its located at /private/etc/hosts). Add the line: 127.0.0.1 myapp.com
  2. Goto your Twitter App Settings and click on the "Settings" tab. Set your callback URL to: http://myapp.com:8000/thanks

Make sure you set the port number and url correctly. Hope that helps!

heri0n
  • 1,459
  • 3
  • 19
  • 33