Search code examples
djangodjango-allauthdjango-rest-auth

Django rest-auth/allauth and weibo integration


I'm trying to use django rest-auth and allauth together for backend authentication process with reactjs frontend application. I'm not sure if I have the setup correctly or if this is a bug. The "uid" does not exist in the response, it only carries token which is passed from the api. I think the uid should be from either existing user or a new one from new users, and "complete_login" should pass those uid along...

Please let me know if anyone has any ideas.

Full traceback below:

Traceback:

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_auth/views.py" in post
  81.         self.serializer.is_valid(raise_exception=True)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
  213.                 self._validated_data = self.run_validation(self.initial_data)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validation
  410.             value = self.validate(value)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_auth/registration/serializers.py" in validate
  106.             login = self.get_social_login(adapter, app, token, access_token)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/rest_auth/registration/serializers.py" in get_social_login
  42.         social_login = adapter.complete_login(request, app, token, response=response)

File "/home/ubuntu/panbecopy/src/penv/local/lib/python2.7/site-packages/allauth/socialaccount/providers/weibo/views.py" in complete_login
  20.         uid = kwargs.get('response', {}).get('uid')

Exception Type: AttributeError at /rest-auth/weibo/
Exception Value: 'unicode' object has no attribute 'get'

Solution

  • That means the allauth didn't get the correct user information. You can check the source code (views.py and provider.py) of the social login you used. For example, linkedin,

    def complete_login(self, request, app, token, **kwargs):
        extra_data = self.get_user_info(token)
        return self.get_provider().sociallogin_from_response(
            request, extra_data)
    

    Make sure you used correct parameters (token, kwargs). Pay attention, the token parameters for different social auth plugins are different. For instance, for linkedin it's just string, but for weibo, it's

    uid = kwargs.get('response', {}).get('uid')
    

    That means the 'response' should be a dictionary.

    If you used the correct parameters in the correct format, that should work.

    Hope helps.