0

我沒辦法把我的社會用戶提供django.I關聯創建一個CreateAPIView這需要token_access,token_access_secret和供應商從users.But一切似乎以work.but do_auth方法恰好犯規創建
我用戶社交帳戶。Django的社會權威性

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() 

的其他imlementation:

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工作,它總是返回用戶爲無

感謝。

回答

0

django-social-auth已被棄用,用於python-social-auth。我想你會很樂意轉換並嘗試使用爲該項目提供的大量文檔。

+0

是的,我知道,但問題是,我被迫升級整個項目。我使用Django 1.5和許多第三方。 – TheGreenGoblen

+0

啊,那很好玩。我相信你已經意識到了這一點,但是Django 1.5(和1.6)也被認爲是不受支持的,並且可能不安全。對不起,我忍不住你的情況:) – seawolf

+0

無論如何謝謝你的時間。 – TheGreenGoblen