2012-08-27 131 views
4

我剛更新爲Twython 2.3.4,但現在我的Twitter認證停止工作。它在'auth_props = twitter.get_authentication_tokens()'行失敗。任何想法出了什麼問題?提前致謝!Twython 2.3.4的Twitter認證失敗,錯誤:401,無法驗證oauth簽名和令牌

的Python代碼做Twitter的使用AUTH是Twython如下:

def begin_auth(request): 
    twitter = Twython(
     twitter_token = TWITTER_KEY, 
     twitter_secret = TWITTER_SECRET, 
     callback_url = request.build_absolute_uri(reverse('portnoy.views.thanks')) 
    ) 
    # Request an authorization url to send the user to... 
    auth_props = twitter.get_authentication_tokens() 

我對上面的線以下錯誤:TwythonAuthError:「看來事情不能和您的OAuth垃圾進行驗證錯誤:401消息:無法驗證的OAuth簽名和令牌」

# Then send them over there, durh. 
    request.session['request_token'] = auth_props 
    return HttpResponseRedirect(auth_props['auth_url']) 

def thanks(request, redirect_url='/'): 
    c = RequestContext(request) 
    # for permanent ones and store them... 
    twitter = Twython(
     twitter_token = TWITTER_KEY, 
     twitter_secret = TWITTER_SECRET, 
     oauth_token = request.session['request_token']['oauth_token'], 
     oauth_token_secret = request.session['request_token']['oauth_token_secret'] 
    ) 

    # Retrieve the tokens we want... 
    authorized_tokens = twitter.get_authorized_tokens() 
    request.session['request_tokens'] = authorized_tokens 
    debug('thanks', request.session['request_tokens']) 

    user = User.objects.filter(username=authorized_tokens['screen_name']) 
    if user.exists(): 
     user = user[0] 
     user.backend='django.contrib.auth.backends.ModelBackend'  
     auth.login(request,user) 
    else: 
     return render_to_response('twitter_register.html', c) 
    return HttpResponseRedirect(redirect_url) 
+0

我以前沒有用過twython,我可以說一些好東西:http://code.google.com/p/python-twitter/ –

+0

你可以仔細檢查你的TWITTER_KEY和TWITTER_SECRET嗎? –

+0

TWITTER_KEY和TWITTER_SECRET都是正確的,只需再次檢查;) – Arman

回答

2

我Twython的作者。

什麼版本的請求在引擎蓋下運行?最近出現了一個問題,由於上游漏洞,人們不斷遇到各種與OAuth相關的錯誤。好奇,如果它是相關的...

+0

我正在運行請求0.13.9。 pip安裝請求== 0.13.9 需求已滿足(使用--upgrade升級):/srv/my_project/venv/lib/python2.7/site-packages/requests-0.13.9中的請求== 0.13.9 -py2.7.egg 正在清理... – Arman

+2

它被標記爲解決方案,但我仍然感到困惑。它是導致錯誤的請求版本嗎? –

+0

我第二@KineticStack的問題。它是導致錯誤的請求版本嗎? – Moonwalker

1

我自己遇到了同樣的問題。 如果您在本地進行測試,它似乎不起作用。

這裏是我做了什麼來解決這個問題:

  1. 編輯您的主機文件(在OSX其位於/私營/ etc/hosts文件)。添加行: 127.0.0.1 myapp.com
  2. 轉到您的Twitter應用程序設置,然後單擊「設置」選項卡。將您的回撥URL設置爲: http://myapp.com:8000/thanks

確保正確設置端口號和URL。 希望有所幫助!

相關問題