用twython在django的本地服务器上测试twitter应用程序 [英] Testing the twitter app on local server in django with twython
问题描述
我正在使用 twython库,与twitter python库进行握手。
我正在本地服务器上测试,127.0.0.1:8000
I am using the twython library, to do handshakes with twitter python library. And I am testing things on my local server, 127.0.0.1:8000
这是我第一个为用户生成twitter令牌的django视图。 / p>
This is my first django view, that generates the twitter tokens for users.
def twitter_auth(request):
"""
The view function that initiates the entire handshake.
For the most part, this is 100% drag and drop.
"""
# Instantiate Twython with the first leg of our trip.
twitter = Twython(
settings.TWITTER_KEY,
settings.TWITTER_SECRET,
)
# Then send them over there, durh.
tw_callback_url = request.build_absolute_uri(reverse('social_home'))
twitter_auth = twitter.get_authentication_tokens(callback_url=tw_callback_url)
request.session['twitter_auth'] = twitter_auth
return HttpResponseRedirect(twitter_auth['auth_url'])
从上面的视图,用户被重定向到第二个查看,我希望阅读用户的时间表,我按以下方式进行操作 -
From the above view, the user is redirected to the second view, where I wish to read the timeline of the user, I do it in following manner -
def social_home(request):
oauth_token_secret = request.session['twitter_auth']['oauth_token_secret']
oauth_token = request.session['twitter_auth']['oauth_token']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET, oauth_token, oauth_token_secret)
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
user_tweets = twitter.get_home_timeline()
return render(request, "social_summary.html", {"user_tweets":user_tweets})
但是,在这里,我收到以下错误 -
Twitter API返回401(未经授权),无效或过期令牌
But here, I get the following error - Twitter API returned a 401 (Unauthorized), Invalid or expired token
请帮助我知道我在哪里错了。
Please help me know, where I am wrong.
感谢您的时间。
推荐答案
def social_home(request):
oauth_token_secret = request.session['twitter_auth']['oauth_token_secret']
oauth_token = request.session['twitter_auth']['oauth_token']
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET, oauth_token, oauth_token_secret)
authorized_tokens = twitter.get_authorized_tokens(request.GET['oauth_verifier'])
twitter = Twython(settings.TWITTER_KEY, settings.TWITTER_SECRET, authorized_tokens['oauth_token'], authorized_tokens['oauth_token_secret'])
user_tweets = twitter.get_home_timeline()
return render(request, "social_summary.html", {"user_tweets":user_tweets})
应该工作!祝你好运!
这篇关于用twython在django的本地服务器上测试twitter应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!