django 为什么会出现“错误400:重定向URI不匹配”?

vxbzzdmp  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(87)

我的目标是在我的Django website中实现google authentication。但是它显示,

Access blocked: This app’s request is invalid
You can’t sign in because this app sent an invalid request. You can try again later, or contact the developer about this issue. Learn more about this error
If you are a developer of this app, see error details.
Error 400: redirect_uri_mismatch

***为什么会出现这种情况?***我尝试在本地主机中实现它。给予我一个可以理解的解决方案,这样作为一个初学者,我就可以理解。同样的问题也发生在facebook authentication上。
Google开发者控制台:

  • 受权的JavaScript来源:*
urls1:http://localhost:8000
urls2:http://127.0.0.1:8000
urls3:http://localhost:3000
urls4:http://localhost
  • 授权的重定向URI:*
urls1:http://127.0.0.1:8000/
urls2:http://localhost:8000
urls3:http://localhost:3000
urls4:http://localhost

设置.py:

MIDDLEWARE = [
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

上下文处理器(_P):

'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',

AUTHENTICATION_BACKENDS = [
        'social_core.backends.google.GoogleOAuth2',
        'django.contrib.auth.backends.ModelBackend',
    ]


LOGIN_URL = '/'
LOGIN_REDIRECT_URL = '/'
LOGOUT_URL = '/' 
LOGOUT_REDIRECT_URL = '/'
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = '****'    #security purpose I hide this
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = '****'    #security purpose I hide this

网址.py

path('social-auth/', include('social_django.urls', namespace='social'))

模板:

<a href="{% url 'social:begin' 'google-oauth2' %}"><i class="fab fa-google"></i></a>
bvjxkvbb

bvjxkvbb1#

将这些添加到您的Google主机的授权重定向URI:

http://127.0.0.1:8000/auth/google_oauth2/callback
 
 http://localhost:8000/auth/google_oauth2/callback

相关问题