Search code examples
pythondjangogoogle-oauthpython-social-auth

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


I am using social-app-django GoogleOAuth2 backend and hit a problem with redirect_uri

I managed to setup INSTALLED_APP, AUTHENTICATION_BACKENDS, url.py and add below 3 in settings

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/'

I have http://localhost:8000/api/complete/google-oauth2/ added into my google Authorised redirect URIs.

I triggered auth login by accessing http://127.0.0.1:8000/login/google-oauth2/ (yes, my project use react on frontend, so not using Django templates).

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: ....

The redirect_url in generated auth url is always http://127.0.0.1:8000/complete/google-oauth2/ and if I replace it with the on I configured in Google console, then it works. So I am guessing it must be something settings related.

It looks like redirect url settings does work, any idea what's wrong? Please help!


Solution

  • You can add redirect_uri inside the auth_params along with access_type in settings file.

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