Implement Oauth 2.0 Authentication with Django Framework
Here are the basic steps to integrate a Django web applications with Google Oauth2,0 as the authentication .
I have mentioned all the steps in a very simple and concise way.
All the steps are explained comprehensively.
Installing the library packages
Create a virtual environment and install the below packages
pip install Django==3.1.1
pip install social-auth-app-django==4.0.0
I have created a project called socialproject and an application called app1.
My Project file structure should look like the below:-
Add the installed Apps once the application is created
I have modified the settings to add the application app1 along with social_django , that has come from the package:- social-auth-app-django in the INSTALLED_APPS
INSTALLED_APPS = [
….
‘social_django’,
‘app1’,
]
Add the Google OAuth2 authentication backend
Now add the Authentication Backends as per the below format in your settings.py file:-
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
LOGIN_URL = '/auth/login/google-oauth2/' (This redirects to the login page)
LOGIN_REDIRECT_URL = '/manage/' (This is one of my routes that i will define in the urls.py file,This parameter will be used to redirect once Login is Successful.)LOGOUT_REDIRECT_URL = '/' (This parameter will be used to redirect once Logout is Successful.)lSOCIAL_AUTH_URL_NAMESPACE = 'social' (For reverse redirects used in the urls.py)social_core.backends.google.GoogleOAuth2 is for the Social Google authentication and the django.contrib.auth.backends.ModelBackend is for the Django's user authentication system.
Configuring the Google Authentication API
Navigate to the Google’s Developer Console.
https://developers.google.com/
Create a project (I have created a sample project DjangoOauth2
Click on the Credentials.
Then click on Create Credentials to generate a OAuth 2.0 Client ID
Select the Application Type as Web Application and give a proper name to the Oauth Client ID (In my case i have given the name as oauthclient3
Mention the Authorised URL as http://localhost:8000/complete/google-oauth2/
The Authorised URL will redirect the Login page rendered in any HTML template to the Google Gmail Authentication System.
Give Consent to the created Oauth Client ID with the Developer’s Email contact.
We will conclude the configuration by specifying the SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET and the SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET respectively in the settings.py file.
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET =’Client ID’
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ‘Client secret’
Make sure to enable the Gmail API service for the OAuth Client ID that we have created.
I configure my url file as below:-
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.contrib.auth.views import LogoutView
from app1.views import index,manage
from django.views.generic import TemplateView
urlpatterns = [
path(‘admin/’, admin.site.urls, name=’administrator’),
path(‘’, include(‘social_django.urls’, namespace=’social’)),
path(‘logout/’,LogoutView.as_view(template_name=settings.LOGOUT_REDIRECT_URL),name=’logout’), #I have mentioned LOGOUT_REDIRECT_URL is mentioned in the settings.py file as on of my parameters.
path(‘manage/’, manage, name=’manage’),]
I have a view function that render a small HTML link to redirect me to the Google’s Authentication that will allow the users to authenticate via their emails and use the Web API portal.
In views.url file
from django.shortcuts import render
def manage(request):
return render(request,template_name=’template_manage.htm’)
I have an HTML file under the templates folder of my app(app1 in this case).
Contents of my template file that i have renderred here.
<div class=”container pt-5">
<h1>Google OAuth Implementation</h1>
{% if user.is_authenticated %}
<h2>
<a href=”/admin/”> Go To Dashboard</a>
</h2>
<hr>
<a class=”btn btn-primary mt-5" href=”{% url ‘logout’ %}”>Logout</a>
{% else %}
<a class=”btn btn-primary” href=”{% url ‘social:begin’ ‘google-oauth2’ %}”>
Login
</a>
{% endif %}
</div>
Execute the makemigrations and the migrate command respectively.
Execute the runserver and access the route:-http://127.0.0.1:8000/manage/
The Output will look precisely like the below:-
Once the Login is clicked it successfully redirects to the Gmail Authentication system
If you notice it is asking me to login so that it can continue to the app1(The Application that i have created in my main project,and this successfully implements the integration of Google’s Oauth2.0 Authentication with Pthon’s Django Web API Framework.