【问题标题】:Django Twitter OAuth authenticationDjango Twitter OAuth 身份验证
【发布时间】:2011-06-10 12:29:50
【问题描述】:

我正在尝试在我的 Django 应用程序上通过 OAuth 实现 Twitter 身份验证,但在收到 Twitter 回调后我遇到了这个错误:

Traceback:

File "/Library/Python/2.6/site-packages/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/Users/flavioramos/projects/sobo/sobo/../sobo/views.py" in twitter_authenticated
  105.     auth_login(request, user)
File "/Library/Python/2.6/site-packages/django/contrib/auth/__init__.py" in login
  71.     user.save()
File "/Library/Python/2.6/site-packages/django/contrib/auth/models.py" in save
  430.         raise NotImplementedError

Exception Type: NotImplementedError at /login/authenticated
Exception Value: 

我的登录/认证视图如下:

def twitter_authenticated(request):
    token = oauth.Token(request.session['request_token']['oauth_token'],
        request.session['request_token']['oauth_token_secret'])
    client = oauth.Client(consumer, token)

    resp, content = client.request(access_token_url, "GET")
    if resp['status'] != '200':
        print content
        raise Exception("Invalid response from Twitter.")

    access_token = dict(cgi.parse_qsl(content))

    try:
        user = User.objects.get(username=access_token['screen_name'])
    except User.DoesNotExist:
        user = User.objects.create_user(access_token['screen_name'],
            '%s@twitter.com' % access_token['screen_name'],
            access_token['oauth_token_secret'])

        profile = Profile()
        profile.user = user
        profile.oauth_token = access_token['oauth_token']
        profile.oauth_secret = access_token['oauth_token_secret']
        profile.save()


    user = authenticate(username=access_token['screen_name'],password=access_token['oauth_token_secret'])

    auth_login(request, user)

    return HttpResponseRedirect('/')

此代码来自 python-oauth2 示例应用程序。

我必须为此编写自己的身份验证后端吗?

我是 Django 的新手,我们将不胜感激。

谢谢,

【问题讨论】:

    标签: python django twitter oauth


    【解决方案1】:

    不太确定,但我认为

    *auth_login(请求,用户)*

    应该是:login(request, user)

    (当然,这个 login 应该从 django.contib.auth 在您的登录/身份验证视图顶部导入)

    【讨论】:

      【解决方案2】:

      我也有同样的问题。似乎 oauthtwitter 使用的库已经改变。要修补您的本地版本,请执行以下操作:

      在:

      /usr/local/lib/python2.6/dist-packages/oauth_python_twitter-1.0-py2.6.egg/oauthtwitter.py
      

      找到第 37 行并将其更改为:

      Api.__init__(self, access_token.key, access_token.secret)
      

      到:

      Api.__init__(self, consumer_key, consumer_secret, access_token.key, access_token.secret)
      

      现在所有必需的变量都已正确传递。

      【讨论】:

        猜你喜欢
        • 2011-06-18
        • 2011-04-02
        • 2013-07-05
        • 2011-10-05
        • 2013-04-15
        • 2011-05-01
        • 2015-03-22
        • 2015-03-13
        • 2015-05-12
        相关资源
        最近更新 更多