【问题标题】:Error retrieving access token: b'{\n "error": "redirect_uri_mismatch",\n "error_description": "Bad Request"\n}'检索访问令牌时出错:b'{\n "error": "redirect_uri_mismatch",\n "error_description": "Bad Request"\n}'
【发布时间】:2021-04-22 23:33:51
【问题描述】:

尝试使用 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, *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}'

【问题讨论】:

  • 错误解决了吗?我正在使用 dj-rest-auth(rest-auth 的分支)并尝试使用 Google 登录,在成功调用 Google 后,我得到了代码并将其发送到我的视图,但出现了这个错误。
  • 不,它无法解决,所以我直接尝试使用 nuxt auth 包使用 google 进行身份验证。

标签: django django-rest-framework nuxt.js django-allauth django-rest-framework-simplejwt


【解决方案1】:

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

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

如果没有提供任何代码,这将很难回答,但我看到您在 Medium tutorial on this topic 上的评论,我遇到了同样的问题。示例代码将/login 保留在 URL 之外。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多