首页 > 解决方案 > AttributeError:“HTTPError”对象没有属性“消息”

问题描述

我进行了自定义 LinkedIn 身份验证,并且正在使用 python3-LinkedIn。我相信 get_profile() 是导致问题的原因,但不确定原因。

错误来自我的代码还是 API 本身?我怎样才能返回用户信息?

这是我的serializers.py

class LinkedinAuthSerializer(serializers.Serializer):
    oauth2_access_token = serializers.CharField()

    def validate(self, oauth2_access_token):
        user_data = linkedin.Linkedin.validate(oauth2_access_token)
        print(user_data)

        try:
            user_id = user_data['id']
            email = user_data['email']
            name = user_data['name']
            provider = 'facebook'
            return register_social_user(
                provider=provider,
                user_id=user_id,
                email=email,
                name=name
            )
        except Exception as identifier:

            raise serializers.ValidationError(
                'The token  is invalid or expired. Please login again.'
            )

视图.py

class LinkedinSocialAuthView(GenericAPIView):
    serializer_class = LinkedinAuthSerializer

    def post(self, request):
        serializer = self.serializer_class(data = request.data)
        serializer.is_valid(raise_exception=True)
        data = ((serializer.validated_data)['oauth2_access_token'])
        return Response(data, status=status.HTTP_200_OK)

链接的.py

from linkedin import linkedin
class Linkedin:
    @staticmethod
    def validate(oauth2_access_token):
        application = linkedin.LinkedInApplication(token=oauth2_access_token)
        # profile = application.get_profile()
        profile = application.get_profile(selectors=['id', 'first-name'])
        return profile

这是完整的代码:https ://github.com/abinashkarki/rest_framework_authentication

我收到此错误消息:

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\linkedin\utils.py", line 56, in raise_for_error
    response = response.json()
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\models.py", line 898, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\PC\authentication_project\socialauth\views.py", line 41, in post
    serializer.is_valid(raise_exception=True)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
    value = self.validate(value)
  File "C:\Users\PC\authentication_project\socialauth\serializers.py", line 120, in validate
    user_data = linkedin.Linkedin.validate(oauth2_access_token)
  File "C:\Users\PC\authentication_project\socialauth\linkedin.py", line 9, in validate
    profile = application.get_profile(selectors=['id', 'first-name'])
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\linkedin\linkedin.py", line 210, in get_profile
    raise_for_error(response)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python38-32\lib\site-packages\linkedin\utils.py", line 66, in raise_for_error
    raise LinkedInError(error.message)
AttributeError: 'HTTPError' object has no attribute 'message'
[01/Nov/2021 09:50:58] "POST /social-auth/linkedin/ HTTP/1.1" 500 22019

标签: pythondjango-rest-frameworklinkedin-apidjango-socialauth

解决方案


推荐阅读