如何使用Twython将oauth_callback值传递给oauth / request_token [英] How to pass oauth_callback value to oauth/request_token with Twython

查看:186
本文介绍了如何使用Twython将oauth_callback值传递给oauth / request_token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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



这导致Twython get_authorized_tokens 抛出此错误:

 请求:oauth / access_token 

错误:未提供oauth_verifier参数

我有两个问题:



1。如何通过Twython将 oauth_callback 值传递给oauth / request_token?



2 。如何传递 oauth_verifier



我可以得到 oauth_verifier with request.GET ['oauth_verifier']从回调URL,但我不知道从那里使用Twython做什么。我搜索到处都是,但没有找到任何答案,所以我决定发布。这是我的第一篇文章,请善待;)



这是我的代码:

 code> def register_twitter(request):
#使用我们行程的第一段实例化Twython。
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
callback_url = request.build_absolute_uri(reverse('account.views.twitter_thanks'))


#请求授权网址将用户发送给
auth_props = twitter.get_authentication_tokens()

#然后将它们发送到
request.session ['request_token'] = auth_props
return HttpResponseRedirect(auth_props ['auth_url'])


def twitter_thanks(request,redirect_url = settings.LOGIN_REDIRECT_URL):

#现在我们已经从Twitter获得了魔术令牌,我们需要为
#交换永久的代码,并存储...
twitter = Twython(
twitter_token = settings.TWITTER_KEY,
twitter_secret = settings.TWITTER_SECRET,
oauth_token = request.session ['request_token'] ['oauth_token'],
oau th_token_secret = request.session ['request_token'] ['oauth_token_secret'],


#取回令牌
authorized_tokens = twitter.get_authorized_tokens()

#检查twitter用户是否有UserProfile
try:
profile = UserProfile.objects.get(twitter_username = authorized_tokens ['screen_name'])
除了ObjectDoesNotExist:
profile =没有


解决方案

我解决了自己的答案。这个解决方案如果可以帮助别人:



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



get_authorized_tokens 我删除了这一行代码:

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

并添加了以下代码:

  callback_url = self.callback_url或'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。


Twitter just recently made the following mandatory:

1) You must pass an oauth_callback value to oauth/request_token. It's not optional. Even if you have one already set on dev.twitter.com. If you're doing out of band OAuth, pass oauth_callback=oob.

2) You must pass along the oauth_verifier you either received from your executed callback or that you received hand-typed by your end user to oauth/access_token. Here is the twitter thread (https://dev.twitter.com/discussions/16443)

This has caused Twython get_authorized_tokens to throw this error:

Request: oauth/access_token

Error: Required oauth_verifier parameter not provided

I have two questions:

1. How do you pass the oauth_callback value to oauth/request_token with Twython?

2. How do you pass along the oauth_verifier?

I can get the oauth_verifier with request.GET['oauth_verifier'] from the callback url but I have no idea what to do from there using Twython. I've search everywhere but haven't found any answers so I decided to post this. This is my first post so please be kind ;)

Here is my code:

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

解决方案

I solved my own answer. Here is the solution if it can help anyone else:

In the file Twython.py, I added a new parameter oauth_verifier to the Twython class constructor . I get the oauth_verifier value from the callback_url in my twitter_thanks view.

In get_authorized_tokens I removed this line of code:

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

and added the 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 now works like a charm and is OAuth 1.0A compliant.

这篇关于如何使用Twython将oauth_callback值传递给oauth / request_token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆