我正在尝试在我的项目中实现Google登录。当我尝试将social_core的oauth与google一起使用时,我已经有了一个相对有效的登录方案,并使用google帐户登录。它不断抛出social_core.exceptions.AuthMissingParameter错误,这意味着以某种方式根本没有传递状态。
我一直在这里https://fosstack.com/how-to-add-google-authentication-in-django/进行的演练,以此作为实现此目标的基准。当我运行本教程中的代码时(除了一些基本的语法和版本错误),所有内容都像魅力一样运行。但是,当我尝试在项目上实现它时,发生了我无法解决的错误。
我拥有的url.py文件,我只导入了一些文件并添加了行
path('auth/', include('social_django.urls', namespace='social')),
除此之外,我的大多数测试代码都与博客类似:在我的公共文件中是这样的。
AUTHENTICATION_BACKENDS = (
'social_core.backends.open_id.OpenIdAuth', # for Google authentication
'social_core.backends.google.GoogleOpenId', # for Google authentication
'social_core.backends.google.GoogleOAuth2', # for Google authentication
'django.contrib.auth.backends.ModelBackend',
)
目前的情况是,我可以单击登录,将我带到我在Google帐户中键入的登录页面,然后引发以下异常:
Environment:
Request Method: GET
Request URL: http://localhost:3000/auth/complete/google-oauth2/
Django Version: 2.1.7
Python Version: 3.6.5
Installed Applications:
[##Some personal files###
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'markdownx',
'social_django',
'livereload']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_django\utils.py" in wrapper
49. return func(request, backend, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_django\views.py" in complete
33. *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\actions.py" in do_complete
43. user = backend.complete(user=user, *args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\base.py" in complete
40. return self.auth_complete(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\utils.py" in wrapper
259. return func(*args, **kwargs)
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\oauth.py" in auth_complete
388. state = self.validate_state()
File "C:\Users\Daniel\.virtualenvs\project-djMFGD5K\lib\site-packages\social_core\backends\oauth.py" in validate_state
88. raise AuthMissingParameter(self, 'state')
Exception Type: AuthMissingParameter at /auth/complete/google-oauth2/
Exception Value: Missing needed parameter state
我在其中删除了一些更个人的信息,但这些文件目前未使用。我非常确定我的密钥正确无误,因为运行服务器后在终端中将其打印出来时,参数是正确的,并且Google确实向我发送了重定向。
编辑:非常确定这些键是正确的,因为当我删除它们或键入不正确时,我会发现服务器未找到错误而不是缺少参数错误。