我想允许注册用户添加多个Twitter帐户。我按照twython-django的例子,得到了一个用户的工作版本 - 一个推特账号。如果用户再次尝试使用Twitter登录,请再次遵循相同的视图,我收到此错误:Invalid / expired Token
。
我尝试在oauth/authorize
和oauth/authenticate
添加force_login = true,从'request_token'
dict删除request.session
,但{{1}期间仍然出现Invalid Token
错误}}
如何使用twython将多个Twitter帐户与同一用户正确关联?我在这里缺少什么?
这是一个twython-django示例:https://github.com/ryanmcgrath/twython-django/blob/master/twython_django_oauth/views.py
我的观点:
get_authentication_tokens()
更新:可能的解决方案
我改变了我的看法:
def twitter_login(request):
redirect_back_to_url = request.build_absolute_uri()
if 'request_token' not in request.session:
# request authorization tokens
t = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
twitter_secret=settings.TWITTER_CONSUMER_SECRET,
callback_url=redirect_back_to_url)
# Request an authorization url to send the user to...
request_oauth_key = t.get_authentication_tokens()
# signing current session as one with twitter authentication
request.session['request_token'] = request_oauth_key
# redirecting the user to twitter authorization url for authentication
return HttpResponseRedirect(request_oauth_key['auth_url'])
else:
# user authenticated, receiving auth token
t2 = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
twitter_secret=settings.TWITTER_CONSUMER_SECRET,
oauth_token=request.session['request_token'][
'oauth_token'],
oauth_token_secret=request.session['request_token'][
'oauth_token_secret'])
oauth_key = t2.get_authorized_tokens()
# save authorized tokens
# twitter oauth tokens dont expire
token = Token.objects.get_or_create(account_name=oauth_key['screen_name'],
token=oauth_key['oauth_token'],
secret=oauth_key['oauth_token_secret'])
user = request.user.get_profile()
user.twitter.add(token[0].id)
user.save()
logger.info('Successfully acquired twitter oauth token.')
return HttpResponseRedirect(reverse('profile'))
还不确定这与它有什么关系。我在twython.py def twitter_login(request):
redirect_back_to_url = request.build_absolute_uri()
if 'request_token' not in request.session:
# request authorization tokens
t = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
twitter_secret=settings.TWITTER_CONSUMER_SECRET,
callback_url=redirect_back_to_url)
# Request an authorization url to send the user to...
request_oauth_key = t.get_authentication_tokens()
# signing current session as one with twitter authentication
request.session['request_token'] = request_oauth_key
# redirecting the user to twitter authorization url for authentication
return HttpResponseRedirect(request_oauth_key['auth_url'])
else:
# user authenticated, receiving auth token
t2 = Twython(twitter_token=settings.TWITTER_CONSUMER_KEY,
twitter_secret=settings.TWITTER_CONSUMER_SECRET,
oauth_token=request.session['request_token'][
'oauth_token'],
oauth_token_secret=request.session['request_token'][
'oauth_token_secret'])
oauth_key = t2.get_authorized_tokens()
if 'screen_name' not in oauth_key:
del request.session['request_token']
request.session.modified = True
return HttpResponseRedirect(reverse('twitter_login'))
# save authorized tokens
# twitter oauth tokens dont expire
token = Token.objects.get_or_create(account_name=oauth_key['screen_name'],
token=oauth_key['oauth_token'],
secret=oauth_key['oauth_token_secret'])
user = request.user.get_profile()
user.twitter.add(token[0].id)
user.save()
logger.info('Successfully acquired twitter oauth token.')
return HttpResponseRedirect(reverse('profile'))
中的第272行之后添加了。但是,正如我所说,我不确定这是否有任何影响,根据https://dev.twitter.com/docs/api/1/post/oauth/request_token强制登录不是可选的args之一。
这是伏都教。大声笑。 告诉我它是否完全是垃圾。
答案 0 :(得分:1)
twython-django
不是为支持多个帐户关联而构建的(它也不是在Django 1.5上,所以要小心它,直到它是更新〜)。
您需要执行OP所做的操作,并为匹配用户的Token
设置单独的表,然后通过提取相应的令牌来处理他们当前正在使用的帐户。 OP使用force_login
似乎也有效,因为虽然它没有记录,但我相信它仍然有效(根据 this thread ,除非我误读它 - 如果我是,我希望得到纠正。)
我不认为这个答案会被接受,因为我并没有真正解决任何问题,但如果有其他人遇到这个问题,我希望能留下比上述说明更清晰的东西。希望没关系!