我正在尝试使用Django Social Auth实现Twitter登录。我添加了一个部分管道,我使用表单(DOB,电子邮件等)从用户那里收集额外的详细信息。
我的问题是,如果用户已经存在,我想跳过此管道。但是,当我尝试这样做时,我得到一个AuthTokenError“令牌错误:丢失未经授权的令牌”,我无法弄清楚原因。
以下是导致问题的代码:
def gather_extra_data(backend, details, uid, request, user=None, *args, **kwargs):
social_user = UserSocialAuth.get_social_auth(backend.name, uid)
if social_user:
return redirect('socialauth_complete', backend.name)
if not details.get('email'):
if not request.session.get('saved_email'):
return redirect(request_extra, backend=backend.name)
else:
details['email'] = request.session['saved_email']
details['password'] = request.session['password']
details['first_name'] = request.session['first_name']
details['last_name'] = request.session['last_name']
details['dob'] = request.session['dob']
details['gender'] = request.session['gender']
details['avatar_url'] = request.session['avatar_url']
答案 0 :(得分:0)
尝试这种方式:
def gather_extra_data(backend, details, uid, request, user=None, is_new=False, *args, **kwargs):
If not user is None and is_new: #is_new is your missing argument.
if not details.get('email'):
if not request.session.get('saved_email'):
return redirect(request_extra, backend=backend.name)
else:
details['email'] = request.session['saved_email']
details['password'] = request.session['password']
details['first_name'] = request.session['first_name']
details['last_name'] = request.session['last_name']
details['dob'] = request.session['dob']
details['gender'] = request.session['gender']
details['avatar_url'] = request.session['avatar_url']
答案 1 :(得分:0)
您应该在创建用户并关联社交帐户后(在social_auth.backends.pipeline.social.associate_user
条目之后)放置您的管道条目,然后您可以尝试使用以下代码:
def gather_extra_data(social_auth, user, details, request, is_new=False, *args, **kwargs):
if is_new:
if request.session.get('saved_email') is None:
return redirect(request_extra, backend=backend.name)
else:
details['email'] = request.session['saved_email']
details['password'] = request.session['password']
details['first_name'] = request.session['first_name']
details['last_name'] = request.session['last_name']
details['dob'] = request.session['dob']
details['gender'] = request.session['gender']
details['avatar_url'] = request.session['avatar_url']
请记住在您输入之前添加social_auth.backends.pipeline.misc.save_status_to_session
。