Skip to content

Quickstart

Steps To Follow

a real quick starter !! 🍻

  1. Add the app name - django_gauth in INSTALLED_APPS entry of you project
    • in your django project's settings.py file :
      settings.py
      1
      2
      3
      4
      5
      6
      7
      8
      9
      INSTALLED_APPS = [
          'django.contrib.admin',
          'django.contrib.auth',
          'django.contrib.contenttypes',
          'django.contrib.sessions',
          'django.contrib.messages',
          'django.contrib.staticfiles',
          'django_gauth',
      ]
      
  2. Add required configuration variables

    • in your django project's root ( location : project-name/project-name/ ) :
      settings.py
      ... # ... rest of your settings.py file content
      
      # --- END OF FILE :
      
      GOOGLE_CLIENT_ID= env("GOOGLE_CLIENT_ID")           # << set according to your oauth2 client
      GOOGLE_CLIENT_SECRET= env("GOOGLE_CLIENT_SECRET")   # << set according to your oauth2 client
      GOOGLE_AUTH_FINAL_REDIRECT_URL= None        # defaults to `<host>/gauth/`
      CREDENTIALS_SESSION_KEY_NAME= "credentials" # defaults to `credentials`
      STATE_KEY_NAME= "oauth_state"               # defaults to `oauth_state`
      SCOPE= [
          "https://www.googleapis.com/auth/userinfo.email"    # always preffered
          ,"https://www.googleapis.com/auth/userinfo.profile" # always preffered
          ,"openid"                                           # always preffered
          ,"https://www.googleapis.com/auth/drive"            # based on your usage
      ]
      os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'         # strictly for local-development only
      
    • explainantion :
      • os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' directs the server to accept in-secure ( http ) connections .
  3. configure auth urls

    • in your django project's root ( location : project-name/project-name/ ) :
      urls.py
      from django.contrib import admin
      from django.urls import path, include
      
      urlpatterns = [
          path('admin/', admin.site.urls),
          path('gauth/', include('django_gauth.urls')),
      
          # add your other app's urls
          # ...
      
      ]
      
  4. Now run the application server :

    python3 manage.py runserver 8000
    
    python3 manage.py runserver localhost:8000
    
    • we have shown port 8000 in use, you can replace any port number of your choice in place of 8000 . ( e.g : 5000, 8080 etc ... )

Important Points

  • useually all servers ( wsgi, asgi, uWsgi) runs default on http://127.0.0.1:PORT/ , hence always take care to set the redirect endpoints in your google oauth2 client app in accordance with 127.0.0.1 , don't mistake to consider - localhost , 0.0.0.0 and 127.0.0.1 as same while dealing with redirect uri's .
    • For example : suppose you have set http://localhost:PORT/gauth/google-callback as your redirect uri , then take note of running your django app on localhost only !!