<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elvis Dsouza on Medium]]></title>
        <description><![CDATA[Stories by Elvis Dsouza on Medium]]></description>
        <link>https://medium.com/@elvis-dsouza?source=rss-2be96172e2c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Elvis Dsouza on Medium</title>
            <link>https://medium.com/@elvis-dsouza?source=rss-2be96172e2c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:46:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elvis-dsouza/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[REST Framework + Google OAuth2]]></title>
            <link>https://medium.com/codebreakdown/rest-framework-google-oauth2-e72e7eace6cb?source=rss-2be96172e2c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e72e7eace6cb</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Elvis Dsouza]]></dc:creator>
            <pubDate>Tue, 04 Dec 2018 18:12:31 GMT</pubDate>
            <atom:updated>2018-12-04T18:19:53.493Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQBfY9ZEbBJJRf5-dd4VdA.jpeg" /><figcaption><a href="https://flic.kr/p/nz8PJF">https://flic.kr/p/nz8PJF</a></figcaption></figure><p>Want to allow users to login to your website using Google? Here we will see how to make use of OAuth2 to let users login to our REST based application.</p><p>For this example I am using Django REST Framework (DRF). We will not be diving into the details of DRF or even Django for that matter. If you have worked on writing any web back end code, you should find it quite easy to follow through the rest (hehehe) of this article.</p><p><em>In case you want to learn more about DRF the official documentation is very helpful and beginner friendly. Link : </em><a href="https://www.django-rest-framework.org/"><em>https://www.django-rest-framework.org/</em></a></p><p>The first step is designing a flow for our mix-n-match auth process. Here it goes. We will have an HTML page on the front end which would do the OAuth2 magic using Google’s JavaScript library. On Success, the front end will have an authentication code which we need to send to the back end. The back end server receives this code as a string and uses it to make a request to the google server asking for user details. The response will have all the details required.</p><p>This response is used to get the user’s email address and match it with a user in our database. If a user is found we create a token for the user and send it back to the front end. This token is a proof of authentication with our application.</p><p>Tokens are these encrypted magic strings that contain some data in it. Only the ones who have the key can decrypt it and see the data. So the token can fearlessly travel through the web and no one without the key will be able to get its info.</p><p>So with the design ready, let us start by creating a Django app whose sole purpose is to provide Google login functionality via OAuth2 over REST calls.</p><pre>&gt;&gt; django-admin startapp rest_google_auth</pre><p>Add the following html in your templates folder</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5555e93989a8d63ab8084f2446c0a02a/href">https://medium.com/media/5555e93989a8d63ab8084f2446c0a02a/href</a></iframe><blockquote>Note : <strong>insert-your-client-id.apps.googleusercontent.com</strong> is obviously not a valid client id. You need to register your app with google to get a client id that you can use. Login to <a href="https://console.developers.google.com/">https://console.developers.google.com</a> to create OAuth2 credentials on google.</blockquote><p>What is this javascript code doing? We have a few components here.</p><ol><li>Sign In Button : Google’s platform.js library that we have included takes care of most things. It changes this div class “g-signin2” into a button and connects it to methods that need to be called to fire up the OAuth2 process.</li><li>signOut Method : Does what it says. Signs the user out when clicked. We will call it <em>onclick</em> of another button called “Sign Out”.</li><li>onSignIn Method : This method is called once the OAuth2 process from Google’s end completes. We will get the authentication code provided by google and send it to the backend that we are yet to create.</li></ol><p>Now lets see what the backend should look like.</p><p>I have a Django project setup and DRF installed on it. Let us install google’s python client which provides many utility classes and methods including functions for OAuth2.</p><pre>&gt;&gt; pip install google-api-python-client</pre><p>Now its time to write some python code. First add your OAuth2 key to settings.py along with DRF configuration that will allow token authentication through its classes.</p><pre>SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = &#39;<em>insert-your-client-id</em>.apps.googleusercontent.com&#39;</pre><pre>REST_FRAMEWORK = { ‘DEFAULT_AUTHENTICATION_CLASSES’: ( ‘rest_framework.authentication.TokenAuthentication’, ), ‘DEFAULT_PERMISSION_CLASSES’: ( ‘rest_framework.permissions.IsAuthenticated’, )}</pre><p>Let’s continue to write views and handling logic for the token that our front end is going to send us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7c92e69684ab1a9e7a519c22f8553d9/href">https://medium.com/media/b7c92e69684ab1a9e7a519c22f8553d9/href</a></iframe><p>There are two things happening here. First, we validate the authentication code with google that was received. We will use their client library. It is super easy.</p><pre>idinfo = id_token.verify_oauth2_token(request.POST[&#39;code&#39;], requests.Request(), client_id)</pre><p>Second, we find the user from our database by matching email address received from google. Then we generate our application user token that uniquely identifies the user in our database. This is also quite easy with DRF’s auth_token app.</p><pre>token, _ = Token.objects.get_or_create(user=user)</pre><blockquote>Note : When accessing the local dev server use <em>localhost</em> instead of <em>127.0.01.</em><br>Google’s OAuth has issues with requests coming from a page hosted at an IP with no DNS name.</blockquote><p>This obtained token is now sent to the front end as a response and it has to be passed to the back end when authorization is required on some action. Tokens are easy and secure solution for remembering authentication in a stateless system like your REST app.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e72e7eace6cb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codebreakdown/rest-framework-google-oauth2-e72e7eace6cb">REST Framework + Google OAuth2</a> was originally published in <a href="https://medium.com/codebreakdown">CodeBreakDown</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>