检索访问令牌时出错:b'{\n "error": "redirect_uri_mismatch",\n "error_description": "Bad Request"\n}'

问题描述

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

一个错误

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

通过将 SITE_ID = 1 更改为 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,**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,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}'

解决方法

在 Django 视图中设置重定向 URL 以匹配 OAuth 客户端 (Google) 中配置的重定向 URI:

rand()

如果不提供任何代码,这将很难回答,但我看到您对 Medium tutorial on this topic 的评论,我遇到了同样的问题。示例代码在 URL 中留下了 class GoogleLogin(SocialLoginView): authentication_classes = [] adapter_class = GoogleOAuth2Adapter callback_url = "http://localhost:3000/login" # <--- make sure this line is correct! client_class = OAuth2Client