social-app-django - 为 GoogleOAuth2 设置重定向 URL 不工作?

social-app-django - Setup REDIRECT URL for GoogleOAuth2 not working?

我正在使用 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/ 触发了身份验证登录(是的,我的项目在前端使用 React,所以没有使用 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: ....

生成的 auth url 中的 redirect_url 始终是 http://127.0.0.1:8000/complete/google-oauth2/,如果我将其替换为我在 Google 控制台中配置的 on,则它可以工作。所以我猜这一定是一些设置相关的东西。

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

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

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