Question

I created a Client ID and Client Secret from the google cloud api console and added a record in the Social apps table for django-allauth

I also added WEB ORIGIN:

  1. mysite.com (prod)
  2. http://localhost:8000 (dev)

and REDIRECT URI

  1. http:mysite.com/accounts/google/login/callback/ (prod)
  2. localhost:8000/accounts/google/login/callback/ (dev)

in the google api console.

Sign in with Google works great in development and redirects to the localhost callback url on successful sign-in. But I'm getting a redirect_uri_mismatch error in prod.

These are the error details from the google error page:

Request Details

cookie_policy_enforce=false
scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email
response_type=code
access_type=online
redirect_uri=http://127.0.0.1:8000/accounts/google/login/callback/
state=SOME_RANDOM_NUMBER
display=page
client_id=MY_CLIENT_ID

The redirect_uri is still set to 127.0.0.1 instead of http:mysite.com/accounts/google/login/callback/

So how do I set the proper redirect_uri?

This is my settings.py pertaining to django-allauth

INSTALLED_APPS = (
    #the usual stuff
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
)

import django.conf.global_settings as DEFAULT_SETTINGS

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
    "allauth.account.context_processors.account",
    "allauth.socialaccount.context_processors.socialaccount",
)

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
)

ACCOUNT_EMAIL_REQUIRED = True

LOGIN_REDIRECT_URL = "/"

Here's urls.py

urlpatterns = patterns('',

  url(r'^accounts/', include('allauth.urls')),

)

I haven't made any other django-allauth settings. I saw the docs and couldn't find where to make the change.

Was it helpful?

Solution

I found that the issue occurs because the nginx proxy, which sits in front of the python app server, sets the HTTP Host header to localhost.

So when allauth tries to do request.build_absolute_uri, HTTP_HOST is localhost.

So I set the Setting proxy_set_header in the nginx configuration file which fixed the issue.

proxy_set_header Host $http_host;

Also see Facebook oauth authentication is redirecting to localhost instead of to my domain for the same issue in a different avatar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top