我无法将我的社交用户与django关联。我创建了一个CreateAPIView,它从用户那里获取了token_access,token_access_secret和provider.But一切似乎都有效。但是do_auth方法并没有完全创建
我的用户社交帐户。
views.py
class SocialSignUp(CreateAPIView):
# permission_classes = ()
queryset = User.objects.all()
serializer_class = SocialUserRegistrationSerializer
# social_serializer = SocialUserRegistrationSerializer
ee = None
def create(self, request, *args, **kwargs):
prov = request.DATA['provider']
redirect = request.path
if request.user.is_authenticated():
authed_user = request.user
else:
authed_user = AnonymousUser()
backend = get_backend(name=prov, request=request, redirect=redirect)
serializer = self.serializer_class(data=request.QUERY_PARAMS)
if isinstance(backend, BaseOAuth):
print 'BaseOAUTH 1'
# Twitter, for example, uses OAuth1 and requires that you also pass
# an `oauth_token_secret` with your authentication request
token = {
'oauth_token': request.DATA['access_token'],
'oauth_token_secret': request.DATA['access_token_secret'],
}
elif isinstance(backend, BaseOAuth2):
# We're using oauth's implicit grant type (usually used for web and mobile
# applications), so all we have to pass here is an access_token
print 'BaseOAUTH 2'
token = request.DATA['access_token']
print backend.user_data(request.DATA['access_token'])
print backend.get_scope()
try:
# if `authed_user` is None, python-social-auth will make a new user,
# else this social account will be associated with the user you pass in
user = backend.do_auth(access_token=token, )
print user
except AuthAlreadyAssociated:
# You can't associate a social account with more than user
return Response({"errors": "That social media account is already in use"},
status=status.HTTP_400_BAD_REQUEST)
if user and user.is_active:
# if the access token was set to an empty string, then save the access token
# from the request
auth_created = user.social_auth.get(provider=prov)
if not auth_created.extra_data['access_token']:
# Facebook for example will return the access_token in its response to you.
# This access_token is then saved for your future use. However, others
# e.g., Instagram do not respond with the access_token that you just
# provided. We save it here so it can be used to make subsequent calls.
auth_created.extra_data['access_token'] = token
auth_created.save()
# Set instance since we are not calling `serializer.save()`
serializer.instance = user
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED,
headers=headers)
else:
return Response({"errors": "Error with social authentication"},
status=status.HTTP_400_BAD_REQUEST)
我的序列化程序类
class SocialUserRegistrationSerializer(serializers.Serializer):
"""
Serializer to receive social auth for python-social-auth
"""
access_token = serializers.CharField()
access_token_secret = serializers.CharField(required=False)
provider = serializers.CharField()
另一个例子:
def create(self, request, *args, **kwargs):
prov = request.DATA['provider']
redirect = request.path
backend = get_backend(name=prov, request=request, redirect=redirect)
request.social_auth_backend = backend
access_token = request.DATA['access_token']
if hasattr(request, 'user'):
if request.user.is_authenticated():
user = request.user
else:
user = None
user = None
try:
if prov == "google-oauth2":
test_response = googleapis_profile(GOOGLEAPIS_PROFILE, access_token)
# gender = test_response.get('gender')
# email = test_response.get('email')
# full_name = test_response.get('familly_name') + test_response.get('given_name')
if test_response is None:
return Response({'success': False, 'detail': "bad access_token"}, status=status.HTTP_400_BAD_REQUEST)
user = backend.do_auth(access_token, user=user)
print user
my_user = user
user_serializer = SocialUserRegistrationSerializer(user)
return Response({'success': True, 'detail': user_serializer.data})
except Exception as e:
return Response({'success': False, 'detail': e}, status = status.HTTP_400_BAD_REQUEST)
我不知道如何让do_auth工作,它总是将用户返回为无
感谢。