Obtaining access token error 401 in Twython

I am trying to veify Twitter account of user via Twython

def twitter_view(request):
twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens(callback_url='http://127.0.0.1:8000/confirm/', force_login=True)

request.session['oauth_token'] = auth['oauth_token']
request.session['oauth_token_secret'] = auth['oauth_token_secret']
return HttpResponseRedirect(auth['auth_url'])


def redirect_view(request):
oauth_verifier = request.GET['oauth_verifier']
twitter = Twython(APP_KEY, APP_SECRET)
final_step = twitter.get_authorized_tokens(oauth_verifier)
request.user.twitter_oauth_token = final_step['oauth_token']
request.user.twitter_oauth_token_secret = final_step['oauth_token_secret']
request.user.save()

return redirect('twitterapp:homepage')

I am getting

Twitter API returned a 401 (Unauthorized), Invalid / expired Token

Traceback (most recent call last):

File "/Users/bharatagarwal/my-venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request)

File "/Users/bharatagarwal/my-venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/bharatagarwal/projects/twitterproject/mysite/twitterapp/views.py", line 100, in redirect_view
final_step = twitter.get_authorized_tokens(str(oauth_verifier))

File "/Users/bharatagarwal/my-venv/lib/python2.7/site-packages/twython/api.py", line 379, in get_authorized_tokens ken'), error_code=response.status_code)

TwythonError: Twitter API returned a 401 (Unauthorized), Invalid / expired To ken


On the second Twython instantiation, you have to include the OAUTH_TOKEN and the OAUTH_SECRET_TOKEN obtained in the first step.

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)    

It is returning Invalid Token because the instantiation you're using didn't include the tokens you received.

链接地址: http://www.djcxy.com/p/55918.html

上一篇: 'WSGIRequest'对象没有属性'构建

下一篇: 在Twython中获取访问令牌错误401