Twitter最近刚刚强制执行以下操作:
1) 您必须将oauth_callback
值传递给 oauth/request_token。这不是可选的。即使你已经在 dev.twitter.com 上设置了一个。如果您正在执行带外 OAuth,请通过oauth_callback=oob
.
2) 您必须将oauth_verifier
您从执行的回调中收到的或您收到的最终用户手动输入的信息传递给 oauth/access_token。这是推特线程(https://dev.twitter.com/discussions/16443)
这导致 Twythonget_authorized_tokens
抛出此错误:
Request: oauth/access_token
Error: Required oauth_verifier parameter not provided
我有两个问题:
1. 如何oauth_callback
使用 Twython 将值传递给 oauth/request_token?
2.你是如何传递的oauth_verifier
?
我可以oauth_verifier
从回调 url 获取 with request.GET['oauth_verifier'] 但我不知道从那里使用 Twython 做什么。我到处搜索,但没有找到任何答案,所以我决定发布这个。这是我的第一篇文章,所以请善待;)
这是我的代码:
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