3

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
4

1 回答 1

2

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

在文件 Twython.py 中,我向oauth_verifierTwython 类构造函数添加了一个新参数。我从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。

于 2013-04-07T00:30:51.057 回答