首页 > 解决方案 > social-app-django - 为 GoogleOAuth2 设置重定向 URL 不起作用?

问题描述

我正在使用 social-app-django GoogleOAuth2 后端并遇到了 redirect_uri 的问题

我设法设置 INSTALLED_APP、AUTHENTICATION_BACKENDS、url.py 并在设置中添加以下 3

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY='MY KEY'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET='MY SECRET'
LOGIN_REDIRECT_URL='http://localhost:8000/api/auth/complete/google-oauth2/'

我已将http://localhost:8000/api/complete/google-oauth2/添加到我的 google 授权重定向 URI 中。

我通过访问http://127.0.0.1:8000/login/google-oauth2/触发了身份验证登录(是的,我的项目使用前端反应,所以不使用 Django 模板)。

Problem is I always get this error
Error: redirect_uri_mismatch

The redirect URI in the request, http://127.0.0.1:8000/complete/google-oauth2/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: ....

生成的身份验证 url 中的 redirect_url 始终为http://127.0.0.1:8000/complete/google-oauth2/,如果我将其替换为我在 Google 控制台中配置的 on ,则它可以工作。所以我猜它一定与设置有关。

看起来重定向 url 设置确实有效,知道有什么问题吗?请帮忙!

标签: pythondjangogoogle-oauthpython-social-auth

解决方案


您可以redirect_uri在 auth_params 以及access_type设置文件中添加。

    SOCIALACCOUNT_PROVIDERS = {
    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
            'redirect_uri': 'http://127.0.0.1:8000/<custom-url>'
        }
    }
}

推荐阅读