编程语言
首页 > 编程语言> > python – Twitter版本在Twython 2.3.4上失败,错误:401,无法验证oauth签名和令牌

python – Twitter版本在Twython 2.3.4上失败,错误: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相关的错误.好奇,如果它与…有关…

标签:python,django,oauth,twitter-oauth,twython
来源: https://codeday.me/bug/20190630/1331980.html