4

I'm trying to add google authentication to my project

I installed it:

pip install python-social-auth

and added it in settings.py:

SOCIAL_AUTH_USER_MODEL = 'accounts.CustomUser'##

SOUTH_MIGRATION_MODULES = {
    'default': 'social.apps.django_app.default.south_migrations'
    }

AUTHENTICATION_BACKENDS = (
    'myproject.middleware.AuthenticationCMSBackend',##
    'social.backends.google.GoogleOpenId',
    'social.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

LOGIN_REDIRECT_URL = '/'

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY  = '507847...m.apps.googleusercontent.com'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'SEt-6...0j3'"""

TEMPLATE_CONTEXT_PROCESSORS =  (
    ...
    'django.core.context_processors.request',
    'social.apps.django_app.context_processors.backends',
    'social.apps.django_app.context_processors.login_redirect',
)

As for the SOCIAL_AUTH_GOOGLE_OAUTH2_KEY and secert, I went to https://console.developers.google.com/apis/credentials and created them using localhost url (for testing for now)

I call it in template like this:

<a href="{% url 'social:begin' 'google-oauth2' %}"> login </a>

But I get this error:

Backend not found

What am i doing wrong?

next step, I want to make sure only users I validate can authenticate not anyone with google account, how to do so?

cinoch
  • 131
  • 1
  • 7

1 Answers1

0

In your setting.py

AUTHENTICATION_BACKENDS = (
 # Google
 'social_core.backends.google.GoogleOAuth2',
 # Django
 #'django.contrib.auth.backends.ModelBackend',
)
# Google Keys
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'KEY' # Client ID
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'SECRET_' # Client secret

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'

context processors

'context_processors': [
                ...
                'social.apps.django_app.context_processors.backends',
                'social.apps.django_app.context_processors.login_redirect',
            ],

urls.py

urlpatterns = [
...
url('social/', include('social.apps.django_app.urls', namespace='social')),
...
]

in your template add.

<p><a class="btn btn-lg btn-danger btn-block" href="{% url 'social:begin' 'google-oauth2' %}">Entrar con la cuenta de Google</a></p>
rfedorov
  • 689
  • 7
  • 11
erajuan
  • 2,224
  • 20
  • 31
  • 1
    That's exactly what I'm doing as I posted! what is the difference? what should I correct? – cinoch Feb 15 '16 at 10:46
  • where can i find `SOCIAL_AUTH_GOOGLE_OAUTH2_KEY`? in google api console I have `Client ID` , `Client secret`,`Creation date` but I don't have `auth key`? @erajuan @cinoch – Ali Husham Mar 18 '21 at 07:33