开发者

Getting a 'AttributeError at /login/authenticated', 'User' object has no attribute 'backend'

I'm using the Python-Oauth2 library found here, Django 1.3. I'll end up using the Twitter library located here.

I've searched everything I could on this, but I can't see why I'm getting this error. When someone else asked this question, they were told it was because they didn't call for an authenticate(), but I have that right above the login.

I've managed to figure out that the REASON it is failing is somehow drawn into the call to authenticate() with Django. I confirmed this by commenting out the login- which promptly gets 开发者_运维问答rid of the error. Something about that authenticate, whether it's the profile/user information or something of that nature, is failing.

Here's the code from my views.py:

def twitter_authenticated(request):
    # Step 1. Use the request token in the session to build a new client.
    token = oauth.Token(request.session['request_token']['oauth_token'],
                        request.session['request_token']['oauth_token_secret'])
    client = oauth.Client(consumer, token)

    # Step 2. Request the authorized access token from Twitter.
    resp, content = client.request(access_token_url, "GET")
    if resp['status'] != '200':
        print content
        raise Exception("Invalid response from Twitter.")

    """
    This is what you'll get back from Twitter. Note that it includes the
    user's user_id and screen_name.
    {
        'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M',
        'user_id': '120889797',
        'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD',
        'screen_name': 'heyismysiteup'
    }
    """

    access_token = dict(cgi.parse_qsl(content))

    # Step 3. Lookup the user or create them if they don't exist.
    try:
        user = User.objects.get(username=access_token['screen_name'])
    except User.DoesNotExist:
        # When creating the user I just use their screen_name@twitter.com
        # for their email and the oauth_token_secret for their password.
        # These two things will likely never be used. Alternatively, you
        # can prompt them for their email here. Either way, the password
        # should never be used.
        user = User.objects.create_user(access_token['screen_name'], '%s@twitter.com' % access_token['screen_name'], access_token['oauth_token_secret'])

        # Save our permanent token and secret for later.
        profile = Profile()
        profile.user = user
        profile.oauth_token = access_token['oauth_token']
        profile.oauth_secret = access_token['oauth_token_secret']
        profile.save()

    # Authenticate the user and log them in using Django's pre-built
    # functions for these things.

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

    return HttpResponseRedirect('/')


Actually, the issue was the authenticate call in my authenticate view function was failing due to the password (which is actually the access_token['oauth_token_secret']).

So that we're on the same page, I'm talking about the def twitter_authenticated(request): function, that is included in the python-oauth2 github page itself, in the code example, in the views code section.

Due to the fact that I upgraded everything else, as well, along with Django 1.3, I had the wrong password in the auth_user table.

To summarize: simply look at the Django debugging output when loading your page (I'm assuming that you have DEBUG = True in your settings) and then narrow it down to which exact call. I'm pretty sure the function that fails for you, is the one that failed for me, though, which is

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

So make sure the auth_user table/password field is the correct SHA1 of your access_token['oauth_token_secret'].

The reason I had the wrong one... I got a new computer, so I was installing all of the latest of everything, but didn't carry over the database data from my old computer.


I've got the same issue. I just noticed that the github page for Python-Oauth2 has the following message that's related to the views.py file specifically:

"NOTE: The following code was coded for Python 2.4 so some of the libraries and code here might need to be updated if you are using Python 2.6+."

I think I'm going to try downgrading to 2.5 temporarily just to narrow down whether it's a django 1.3 thing, or python.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜