我使用了基于this question的代码我想记录用户,如果用户没有注册,那么我想注册默认帐户和社交帐户,如默认链接登录所有auth一样。用户已经注册时,此功能正常。
来自view.py的代码
class mobile_facebook_login_without_signup(APIView):
@csrf_exempt
def post(self, request, format='application/json'):
if request.method == "POST": # The method better be a POST
access_token = request.DATA.get('access_token') # Get token
email = request.DATA.get('email') # Get email
print "email = " +str(email) + " - access_token = " +str(access_token)
try:
app = SocialApp.objects.get(provider="facebook")
token = SocialToken(app=app, token=access_token)
# Check token against facebook
login = fb_complete_login(request, app, token)
login.token = token
login.state = SocialLogin.state_from_request(request)
print "login = " +str(login)
# Add or update the user into users table
ret = complete_social_login(request, login)
print "ret = " +str(ret)
data = {}
# Try to get username from email
try:
user = User.objects.get(email=email) # Get User
# Login the user from Django's perspective
#user.backend = 'django_tours.auth_backend.PasswordlessAuthBackend'
user.backend = 'allauth.account.auth_backends.AuthenticationBackend'
user = authenticate(email=user.email)
auth_login(request,user)
except User.DoesNotExist,e:
return Response('failure: %s'%(e), status=status.HTTP_401_UNAUTHORIZED)
return Response( 'sign-in successful ', status=status.HTTP_202_ACCEPTED)
except Exception,e:
return Response('Failure: %s'%(e), status=status.HTTP_401_UNAUTHORIZED)
else:
return Response('failure', status=status.HTTP_401_UNAUTHORIZED)
提前致谢。
答案 0 :(得分:0)
我已经弄清楚了,所有我要做的就是添加以下return语句并且它有效
return Response(data={
'success': True,
'username': request.user.username,
'email': email,
'user_id': request.user.pk,
}, status=status.HTTP_202_ACCEPTED)