问题描述
|
我正在使用在此处找到的Python-Oauth2库Django 1.3。我将最终使用这里的Twitter库。
我已经搜索了所有可以解决的问题,但是我看不到为什么出现此错误。当其他人问这个问题时,他们被告知这是因为他们没有要求authenticate(),但是我在登录名的上方有这个权限。
我设法弄清楚了失败的原因已通过某种方式引入了对Django的authenticate()的调用。我通过注释掉登录来确认这一点,该登录会立即消除错误。无论是个人资料/用户信息还是类似性质的身份验证都失败了。
这是我的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(\'/\')
解决方法
实际上,问题是我的身份验证视图函数中的身份验证调用由于密码(实际上是
access_token[\'oauth_token_secret\']
)而失败。
为了使我们在同一页面上,我在说的是def twitter_authenticated(request):
函数,该函数包含在python-oauth2 github页面本身中,在代码示例中的views代码部分。
由于我同时升级了所有其他内容以及Django 1.3,因此在auth_user
表中输入的密码错误。
总结一下:加载页面时,只需查看Django调试输出(我假设您的设置中有DEBUG = True
),然后将其范围缩小到确切的调用范围。我很确定对您来说失败的功能是对我来说失败的功能
user = authenticate(username=\'blahblah\',password=access_token[\'oauth_token_secret\'])
因此,请确保auth_user表/密码字段是您的“ 1”的正确SHA1。
我输入错误的原因是……我有一台新计算机,所以我要安装所有最新版本的计算机,但是没有从旧计算机中继承数据库数据。
, 我有同样的问题。我刚刚注意到,Python-Oauth2的github页面具有以下消息,该消息特别与views.py文件相关:
\“注意:以下代码是针对Python 2.4编写的,因此,如果您使用的是Python 2.6+,则可能需要更新此处的某些库和代码。”
我认为我将暂时尝试将其降级到2.5,以缩小是django 1.3还是python。