How to pass oauth_callback value to oauth/request_token with Twython
Asked Answered
N

1

3

Twitter just recently made the following mandatory:

1) You must pass an oauth_callback value to oauth/request_token. It's not optional. Even if you have one already set on dev.twitter.com. If you're doing out of band OAuth, pass oauth_callback=oob.

2) You must pass along the oauth_verifier you either received from your executed callback or that you received hand-typed by your end user to oauth/access_token. Here is the twitter thread (https://dev.twitter.com/discussions/16443)

This has caused Twython get_authorized_tokens to throw this error:

Request: oauth/access_token

Error: Required oauth_verifier parameter not provided

I have two questions:

1. How do you pass the oauth_callback value to oauth/request_token with Twython?

2. How do you pass along the oauth_verifier?

I can get the oauth_verifier with request.GET['oauth_verifier'] from the callback url but I have no idea what to do from there using Twython. I've search everywhere but haven't found any answers so I decided to post this. This is my first post so please be kind ;)

Here is my code:

def register_twitter(request):
    # 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('account.views.twitter_thanks'))
    )

    # Request an authorization url to send the user to
    auth_props = twitter.get_authentication_tokens()

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


def twitter_thanks(request, redirect_url=settings.LOGIN_REDIRECT_URL):

    # 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'],
    )

    # Retrieve the tokens
    authorized_tokens = twitter.get_authorized_tokens()

    # Check if twitter user has a UserProfile
    try:
        profile = UserProfile.objects.get(twitter_username=authorized_tokens['screen_name'])
    except ObjectDoesNotExist:
        profile = None
Nataline answered 5/4, 2013 at 18:37 Comment(3)
your reverse('account.views.twitter_thanks') is incorrect, it must be reverse('account:twitter_thanks'). Can you post you url codesSipple
@Sipple Actually reverse('account.views.twitter_thanks') works fine. Here is the url: url(r'^register/twitter/thanks/?$', 'account.views.twitter_thanks', name='twitter_thanks'). It's nice to know a shorthand way of writing the view though, thanks for that.Nataline
Where did you get these codes? Actually, your codes are the same with my twitter login and it's perfectly working.Sipple
N
3

I solved my own answer. Here is the solution if it can help anyone else:

In the file Twython.py, I added a new parameter oauth_verifier to the Twython class constructor . I get the oauth_verifier value from the callback_url in my twitter_thanks view.

In get_authorized_tokens I removed this line of code:

response = self.client.get(self.access_token_url)

and added the following code:

callback_url = self.callback_url or 'oob'
request_args = urllib.urlencode({'oauth_callback': callback_url, 'oauth_verifier':self.oauth_verifier })
response = self.client.post(self.access_token_url, params=request_args)

It now works like a charm and is OAuth 1.0A compliant.

Nataline answered 7/4, 2013 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.