Skip to content

Latest commit

 

History

History
115 lines (99 loc) · 4.94 KB

GoogleOAuth.md

File metadata and controls

115 lines (99 loc) · 4.94 KB

Set Up OAuth API

To set up OAuth you will need a Google Account.
If you do not have one, set one up - Here

  • Google Console

    • Choose Project Name and click "CREATE"
    • Click "OAuth consent screen" in APIs & Services
    • Choose "EXTERNAL" and click "CREATE"
    • Fill in Application Name and click "SAVE"
    • On the Credentials tab click "CREATE CREDENTIALS" -> "OAuth Client ID"
    • Fill in the credentials as below and hit "SAVE"
    • A pop up will appear with and , these are used later
  • Django App

    • Install django-allauth

      pip install django-allauth
    • Add the following to INSTALLED_APPS in Django settings

      INSTALLED_APPS = [
        ...  
        'django.contrib.sites',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.google',
      ]
    • At the bottom of settings specify the allauth backend, site id an social account providers

      # Google OAuth
      AUTHENTICATION_BACKENDS = (
          'django.contrib.auth.backends.ModelBackend',
          'allauth.account.auth_backends.AuthenticationBackend',
      )
      SITE_ID = config('SITE_ID')
      SOCIALACCOUNT_PROVIDERS = {
          'google': {
              'SCOPE': [
                  'profile',
                  'email',
              ],
              'AUTH_PARAMS': {
                  'access_type': 'online',
              }
          }
      }
    • add url

      urlpatterns = [
          path('auth/', include('allauth.urls')),
      ]
    • Make migrations to update db

      $ python manage.py makemigrations
      $ python manage.py migrate    
    • Log into Django admin console

    • Get SITE_ID

      • Open a terminal
      • Set DATABASE_URL on your terminal to url of the Hobby DB created.
        $ export DATABASE_URL=<value-from-config-vars>
      • log into database
        $ heroku pg:psql --app bglynch-test-deployment
      • Get the site id from the id column in the django_site table
        SELECT * from django_site;
          id |                    domain                     |                     name                      
         ----+-----------------------------------------------+-----------------------------------------------
           3 | https://bglynch-test-deployment.herokuapp.com | https://bglynch-test-deployment.herokuapp.com
           1 | example.com                                   | example.com
        
        

      We can see from the table that the id for the app is 3

      Set SITE_ID = 3 in Heroku Config Vars