如何在google上使用python-social-auth?

时间:2016-02-12 16:24:19

标签: django google-authentication python-social-auth django-1.6

我正在尝试将Google身份验证添加到我的项目中

我安装了它:

pip install python-social-auth

并将其添加到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',
)

至于SOCIAL_AUTH_GOOGLE_OAUTH2_KEY和secert,我去https://console.developers.google.com/apis/credentials并使用localhost url创建它们(暂时用于测试)

我在模板中这样称呼它:

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

但是我收到了这个错误:

  

未找到后端

我做错了什么?

下一步,我想确保只有我验证的用户才能对使用Google帐户的任何人进行身份验证,该怎么做?

1 个答案:

答案 0 :(得分:0)

In your setting.py

DT[, wanted:=.GRP, by="fruits,vendor"]          # .GRP is simple group counter
DT[, wanted:=wanted-wanted[1]+1L, by="fruits"]  # reset vendor counter per fruit

context processors

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

SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'

urls.py

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

in your template add.

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