gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:47:30 30 4
gpt4 key购买 nike

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 )

这导致 Twython get_authorized_tokens 抛出此错误:

Request: oauth/access_token

Error: Required oauth_verifier parameter not provided

我有两个问题:

<强>1。如何使用 Twython 将 oauth_callback 值传递给 oauth/request_token?

<强>2。您如何传递 oauth_verifier

我可以使用 request.GET['oauth_verifier'] 从回调 url 获取 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

最佳答案

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

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

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 标准。

关于python - 如何使用 Twython 将 oauth_callback 值传递给 oauth/request_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15841155/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com