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
:
mysite.com
(prod)http://localhost:8000
(dev)and REDIRECT URI
http:mysite.com/accounts/google/login/callback/
(prod)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.
Django-allauth settings.py is used to configure Django-allauth. After installing the package, add django-allauth to INSTALLED_APPS in settings.py to register it. Essentially, you can include any of the providers you need to register.
To integrate Google OAuth features into our app, we will use django-allauth. Then register django-allauth by adding it to INSTALLED_APPS in settings.py. The line allauth.socialaccount.providers.google specifies the OAuth provider since django-allauth supports many OAuth providers.
Create a Django project if you already don’t have one. Install django-allauth using the command pip install django-allauth Add, allauthallauth.account, allauth.socialaccount and all the social login features you need to INSTALLED_APPS section in settings.py. You can view the entire list of supported API's here.
To get more user information from Google, your app needs to be verified. You can integrate Google OAuth into your Django application with Django OAuth packages like django-allauth. You can also integrate other OAuth services similarly using django-allauth.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With