-
Create a Project in Google Developer Console:
- Go to Google Developer Console.
- Create a new project and select it.
-
Configure OAuth Consent Screen:
- Search for "OAuth consent screen" and select it.
- Select "External" user type.
- Fill in the app name, upload a logo (optional), and provide your email.
-
Add Scopes:
- On the second window, select scopes like email and profile information.
-
Create OAuth Credentials:
- Go to the "Credentials" tab.
- Click "Create Credentials" and select "OAuth Client ID".
- Provide a name, set
http://127.0.0.1:8000
in authorized JavaScript origins. - Set
http://localhost:8000/accountsgoogle/login/callback/
in authorized redirect URIs. - Download the
client_id
andclient_secret
in JSON format.
-
Create a New Django Project:
django-admin startproject myproject cd myproject
-
Install
django-allauth
:pip install "django-allauth[socialaccount]"
-
Configure
django-allauth
:- Follow the django-allauth quickstart guide to update your settings.
- Ensure
SOCIALACCOUNT_PROVIDERS
includes Google configuration.
-
Create Users App:
python manage.py startapp users
- Add
users
toINSTALLED_APPS
insettings.py
.
- Add
-
Update URL Configurations:
- In
myproject/urls.py
, include URLs forusers
andallauth
.
- In
-
Setup Views and Templates:
- Create
views.py
andurls.py
in yourusers
app. - In the
templates
folder, createindex.html
and add a Google login link. - Configure template directory in
settings.py
.
- Create
-
Migrate Database and Create Superuser:
python manage.py migrate python manage.py createsuperuser
-
Add a Site:
- Start the server and go to
http://127.0.0.1:8000/admin
. - Add a new site with the domain
127.0.0.1:8000
and a display name.
- Start the server and go to
-
Add Social Application:
- Go to "Social applications" and create a new application.
- Select provider as Google, name it Google.
- Enter the
clientId
andclientSecret
from Google. - Select the site
127.0.0.1:8000
and save.
- Log out of the admin and navigate to the homepage.
- Click the Google login link and complete the authentication process.
- If any error occurs, note the valid redirect URL and update it in the Google Developer Console credentials.
- After successfully redirecting, select an account to log in.
Following these instructions, you can integrate Google OAuth authentication into your Django application. Ensure all configurations are correctly set in both Google Developer Console and Django admin.
For more detailed settings, refer to the django-allauth documentation.