如何使用Twython将oauth_callback值传递给oauth / request_token

时间:2013-04-05 18:37:37

标签: python django twitter twitter-oauth twython

Twitter最近刚刚强制要求:

1)您必须将oauth_callback值传递给oauth / request_token。这不是可选的。即使你已经在dev.twitter.com上设置了一个。如果您正在使用带外OAuth,请传递oauth_callback=oob

2)您必须将您从执行的回调中收到的oauth_verifier或您最终用户手动输入的get_authorized_tokens传递给oauth / access_token。 这是twitter线程(https://dev.twitter.com/discussions/16443

这导致Twython Request: oauth/access_token Error: Required oauth_verifier parameter not provided 抛出此错误:

oauth_callback

我有两个问题:

1。如何使用Twython将oauth_verifier值传递给oauth / request_token?

2。你如何传递oauth_verifier

我可以从回调网址获取带有request.GET ['oauth_verifier']的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 ,但我不知道该怎么做才能使用Twython。我到处搜索但没找到任何答案所以我决定发布这个。这是我的第一篇文章,所以请善待;)

这是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:2)

我解决了自己的答案。如果它可以帮助其他任何人,这是解决方案:

在文件Twython.py中,我向Twython类构造函数添加了一个新参数oauth_verifier。我在twitter_thanks视图中从oauth_verifier获得callback_url值。

get_authorized_tokens我删除了这行代码:

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

并添加了以下代码:

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)

它现在像魅力一样,符合OAuth 1.0A。