使用 Twython Oauth 的 Django 应用程序返回“AnonymousUser”;对象没有属性“backend”;

发布于 2024-12-23 15:55:29 字数 1482 浏览 0 评论 0原文

Django 高级初学者在这里。我正把头撞在墙上,试图弄清楚这个问题。我有一个简单的网络应用程序,它使用绑定到 contrib.auth 的 twython_django_oauth 来注册和登录用户。 (我正在使用 twython 开箱即用,无需任何修改。)我可以通过以下方式注册新用户Twitter 没有问题,这会将他们作为登录用户返回到应用程序。然而,随后尝试登录用户时,会返回此错误:

Traceback:
File "/app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/app/thislist/twython_django_oauth/views.py" in thanks
  80.     login(request, user)
File "/app/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  82.     request.session[BACKEND_SESSION_KEY] = user.backend

Exception Type: AttributeError at /thanks
Exception Value: 'AnonymousUser' object has no attribute 'backend'

我注意到的一个异常是,在遇到此问题的注册用户的 Twitter 帐户中,Web 应用程序两次显示为已授权。顺便说一句,几周前这一切似乎都运转良好。我有两个 Twitter 注册用户,可以毫无问题地登录。在这些帐户中,该应用程序似乎只被授权一次。但是,我似乎无法将应用程序回拨到这些用户注册时的位置,以找出我所做的哪些更改可能导致了问题。如果有人了解为什么这里的工作流程返回 AnonymousUser,尽管用户似乎已使用适当的凭据注册,我将不胜感激您的来信!

更新:我已经找到了问题的原因。每次用户在成功输入 Twitter 凭据后被重定向回应用程序时,twython_django 都会尝试使用新的“oauth_token_secret”让他/她登录,而不是获取注册期间生成并存储在 Web 应用程序的 Twitter 配置文件数据库中的秘密令牌。因此,django 无法对用户进行身份验证。所以问题是:为什么这不

    try:
    user = User.objects.get(username = authorized_tokens['screen_name'])

生成带有存储的秘密令牌的用户对象。

Django advanced beginner here. I'm banging my head against the wall trying to figure this one out. I have a simple webapp that uses twython_django_oauth tied into contrib.auth to register and login users. (I'm using twython out of the box with no modifications.) I can register new users via Twitter without a problem, which returns them to the app as a logged in user. Subsequent attempts to log in the user, however, returns this error:

Traceback:
File "/app/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/app/thislist/twython_django_oauth/views.py" in thanks
  80.     login(request, user)
File "/app/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
  82.     request.session[BACKEND_SESSION_KEY] = user.backend

Exception Type: AttributeError at /thanks
Exception Value: 'AnonymousUser' object has no attribute 'backend'

The one anomaly I notice is that in the Twitter accounts of the registered users that encounter this problem, the webapp shows up as authorized twice. Incidentally, this all seemed to be working fine a few weeks ago. I have two registered Twitter users that can log in without a problem. In those accounts, the app appears to be authorized only once. However, I can't seem to dial back the app to the point when these users registered to figure out what changes I made that may have caused the problem. If anyone has any insight into why the workflow here is returning AnonymousUser despite the user seeming to be registered with the appropriate credentials, I'd appreciate hearing from you!

Update: I've zeroed in on the cause of the problem. Each time the user is redirected back to the app after successfully entering their Twitter credentials, twython_django tries to log him/her in using a new 'oauth_token_secret' rather than grabbing the secret token generated during registration and stored in the webapp's Twitter profile database. As a result, django can't authenticate the user. So the question is: Why doesn't this

    try:
    user = User.objects.get(username = authorized_tokens['screen_name'])

generate a user object with the stored secret token.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2024-12-30 15:55:29

当您第一次登录时,示例代码会创建一个新的 django 用户,用户名=authorized_tokens['screen_name'],密码=authorized_tokens['oauth_token_secret']。
第二次登录时,authorized_tokens['oauth_token_secret']发生变化,因此如果使用更改后的token进行身份验证,则会因密码错误而返回AnonymousUser。

要使其正常工作,请在后面添加以下行:

user = User.objects.get(username = authorized_tokens['screen_name'])
user.set_password(authorized_tokens['oauth_token_secret'])
user.save()

希望这个答案很清楚。请随时提问

When you are logging in for the first time, the example code creates a new django user with username=authorized_tokens['screen_name'] and password = authorized_tokens['oauth_token_secret'].
The second time when you login, the authorized_tokens['oauth_token_secret'] changes, so if you use the changed token to authenticate, it will return AnonymousUser since the password is wrong.

To make it work, add the following lines after:

user = User.objects.get(username = authorized_tokens['screen_name'])
user.set_password(authorized_tokens['oauth_token_secret'])
user.save()

Hope this answer is clear. Please feel free to ask questions

等风来 2024-12-30 15:55:29

我是 Twython(以及相关的 Twython-Django 包)的作者。

我现在已经仔细研究了你的问题,但我有点不知道问题是什么。这是我第一次听说它,我认为 Django 的最新版本中没有任何改变会影响这一点(我仔细检查了authenticate()和相关内容以确保......)。

不过,我很乐意帮助调试 - 就像快速健全性检查一样,您是否尝试过打开控制台并使用示例数据手动运行authenticate()?最终,这似乎就是 AnonymusUser 出现问题的原因。缩小这里的范围会有所帮助,因为问题实际上并不在于 try/ except - 所做的只是检查数据库中是否存在用户,如果不存在则创建它们。它与身份验证完全无关。 :)

如果我可以帮助您调试,请告诉我,如果您仍然需要任何帮助,请随时与我联系!

I'm the author of Twython (and the related Twython-Django package).

I've looked over your question for a bit now, and I'm a bit lost as to what the issue is. This is the first I've heard of it, and I don't think anything's changed in recent releases of Django that'd affect this (I double checked authenticate() and the related to be sure...).

I'm happy to help debug, though - just as a quick sanity check, have you tried opening up a console and manually running through authenticate() with sample data? Ultimately, that's what's producing the issue with an AnonymusUser, it would seem. Narrowing the scope here would help, as the issue isn't really with the try/except - all that does is check if a User exists in the DB, and then if not creates them. It doesn't relate to authentication at all. :)

Let me know if I can help you debug, and feel free to reach out to me personally if you still need any help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文