Twython 2.3.4上的Twitter身份验证失败,错误为:401,无法验证oauth签名和令牌



我刚刚更新到Twython 2.3.4,但现在我的Twitter身份验证停止工作。它在"auth_props=twitter.get_authentication_tokens()"行失败。知道出了什么问题吗?提前感谢!

使用Twython进行Twitter身份验证的python代码如下:

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)

我是Twython的作者。

你在幕后运行的是什么版本的请求?最近出现了一个问题,由于上游错误,人们不断遇到各种与OAuth相关的错误。很好奇是否与此有关。。。

我自己也遇到了同样的问题。如果你在本地测试,它似乎不起作用。

以下是我修复它的方法:

  1. 编辑您的主机文件(在OSX上,位于/private/etc/hosts)。添加行:127.0.0.1 myapp.com
  2. 转到您的Twitter应用程序设置,然后单击"设置"选项卡。将您的回调URL设置为:http://myapp.com:8000/thanks

请确保正确设置了端口号和url。希望能有所帮助!

最新更新