python - How to pass oauth_callback value to oauth/request_token with Twython -
twitter made following mandatory:
1) must pass oauth_callback
value oauth/request_token. it's not optional. if have 1 set on dev.twitter.com. if you're doing out of band oauth, pass oauth_callback=oob
.
2) must pass along oauth_verifier
either received executed callback or received hand-typed end user oauth/access_token. here twitter thread (https://dev.twitter.com/discussions/16443)
this has caused twython get_authorized_tokens
throw error:
request: oauth/access_token error: required oauth_verifier parameter not provided
i have 2 questions:
1. how pass oauth_callback
value oauth/request_token twython?
2. how pass along oauth_verifier
?
i can oauth_verifier
request.get['oauth_verifier'] callback url have no idea there using twython. i've search everywhere haven't found answers decided post this. first post please kind ;)
here code:
def register_twitter(request): # instantiate twython 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 authorization url send user auth_props = twitter.get_authentication_tokens() # send them on there request.session['request_token'] = auth_props return httpresponseredirect(auth_props['auth_url']) def twitter_thanks(request, redirect_url=settings.login_redirect_url): # we've got magic tokens twitter, need exchange # permanent ones , 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 tokens authorized_tokens = twitter.get_authorized_tokens() # check if twitter user has userprofile try: profile = userprofile.objects.get(twitter_username=authorized_tokens['screen_name']) except objectdoesnotexist: profile = none
i solved own answer. here solution if can else:
in file twython.py, added new parameter oauth_verifier
twython class constructor . oauth_verifier
value callback_url
in twitter_thanks view.
in get_authorized_tokens
removed line of code:
response = self.client.get(self.access_token_url)
and added 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 works charm , oauth 1.0a compliant.
Comments
Post a Comment