3

尝试使用 Django rest + allauth + dj-rest-auth 从 Nuxt App 进行身份验证

第一个错误

allauth.socialaccount.models.SocialApp.DoesNotExist:SocialApp 匹配查询不存在。

通过更改 SITE_ID = 1 TO 2 解决了这个问题

其次,我收到此错误

Traceback (most recent call last):
      File "C:\Users\xyz\django-env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\xyz\django-env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\xyz\django-env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
        return view_func(*args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\django\views\generic\base.py", line 71, in view
        return self.dispatch(request, *args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\django\utils\decorators.py", line 45, in _wrapper
        return bound_method(*args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\django\views\decorators\debug.py", line 76, in sensitive_post_parameters_wrapper
        return view(request, *args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\dj_rest_auth\views.py", line 48, in dispatch
        return super(LoginView, self).dispatch(*args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
        response = self.handle_exception(exc)
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
        self.raise_uncaught_exception(exc)
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
        raise exc
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
        response = handler(request, *args, **kwargs)
      File "C:\Users\xyz\django-env\lib\site-packages\dj_rest_auth\views.py", line 138, in post
        self.serializer.is_valid(raise_exception=True)
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
        self._validated_data = self.run_validation(self.initial_data)
      File "C:\Users\xyz\django-env\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
        value = self.validate(value)
      File "C:\Users\xyz\django-env\lib\site-packages\dj_rest_auth\registration\serializers.py", line 117, in validate
        token = client.get_access_token(code)
      File "C:\Users\xyz\django-env\lib\site-packages\allauth\socialaccount\providers\oauth2\client.py", line 91, in get_access_token
        raise OAuth2Error("Error retrieving access token: %s" % resp.content)
    allauth.socialaccount.providers.oauth2.client.OAuth2Error: Error retrieving access token: b'{\n  
    "error": "redirect_uri_mismatch",\n  "error_description": "Bad Request"\n}'
4

1 回答 1

1

Set redirect URL in the Django view to match the redirect URI configured in the OAuth client (Google):

class GoogleLogin(SocialLoginView):
    authentication_classes = []
    adapter_class = GoogleOAuth2Adapter
    callback_url = "http://localhost:3000/login" # <--- make sure this line is correct!
    client_class = OAuth2Client

This would be hard to answer without any code provided, but I saw your comment on the Medium tutorial on this topic, and I ran into the same problem. The example code leaves /login off the URL.

于 2021-05-14T18:37:28.923 回答