我正在使用Twython为使用Django构建的网站实现OAuth功能。我希望用户能够使用他们的Twitter凭证进行注册和登录。
我通过稍微调整Twython Git存储库中的示例,创建了用于登录和注册twitter信息的基本视图。
我的问题是,用户目前可以在Firefox和Chrome中使用Twitter OAuth进行注册,但不能在Safari中进行注册。我实现此功能的观点是:
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()
# 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)
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)
在第二个视图中,谢谢,我获取了authorized_tokens并将它们存储在会话中,因此可以在未注册用户所指向的twitter_register.html页面中轻松访问它们,从而允许未注册的用户注册其Twitter信息。
下面提供了Safari中失败行的twitter_register视图代码:
def twitter_register(request):
c = RequestContext(request)
if request.method == 'POST':
email = request.POST.get('email')
password = request.POST.get('password')
if not SafeContact.objects.filter(email=email).exists():
return HttpResponseRedirect('/')
if (User.objects.filter(email=email).exists()):
c['warning'] = "Looks like that email is already in use"
return render_to_response('twitter_register.html', c)
#debug('twitter_register', request.session['request_tokens'])
twitter = Twython(
twitter_token = TWITTER_KEY,
twitter_secret = TWITTER_SECRET,
oauth_token = request.session['request_tokens']['oauth_token'],
oauth_token_secret = request.session['request_tokens']['oauth_token_secret']
)
debug('twitter_register', request.session['request_tokens'])
twitter_user_dict = twitter.showUser(screen_name=request.session['request_tokens']['screen_name'])
所有这些都适用于Firefox和Chrome,但在Safari中失败。特别是,在Safari中,此代码在最后提供的twitter_register行中失败:
twitter.showUser(screen_name=request.session['request_tokens']['screen_name']).
产生以下错误:
The error is KeyError: 'screen_name'.
调试语句debug('thanks',request.session ['request_tokens'])在Firefox和Chrome中按预期打印authorized_tokens,但出于某种原因在Safari中打印2x。它第一次打印时,会按预期显示授权令牌字典,但在第二次打印时会显示
thanks: {'<?xml version': '"1.0" encoding="UTF-8"?>\\n<hash>\\n <error>Invalid / expired Token</error>\\n <request>/oauth/access_token</request>\\n</hash>\\n'}
我很困惑,因为这似乎只发生在Safari中(我在v.5.1.5上测试)。
有关如何解决此问题的任何建议都将受到高度赞赏。感谢您花时间看看这个!
答案 0 :(得分:0)
似乎“感谢”视图正在执行两次。
您的twitter_register视图中的以下行是否可能导致它?
if not SafeContact.objects.filter(email=email).exists():
return HttpResponseRedirect('/')