rha*_*0dy 80
首先,您必须为Google+创建OAuth凭据.
现在让我们添加python-social-auth到您的Django应用程序中.
python-social-auth时pip设置适当的Django设置:
'social.apps.django_app.default'到INSTALLED_APPS:SOCIAL_AUTH_GOOGLE_OAUTH2_KEY和SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET设置.客户端密钥是Google开发人员控制台中"凭据"屏幕中列出的"客户端ID",以".apps.googleusercontent.com"结尾.只在点之前取得部分.该秘密被列为"客户机密".确保您已AUTHENTICATION_BACKENDS明确定义了设置,并且它包含'social.backends.google.GoogleOAuth2'.一个例子是:
AUTHENTICATION_BACKENDS = (
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend')
Run Code Online (Sandbox Code Playgroud)定义SOCIAL_AUTH_PIPELINE设置为详细蟒蛇-社会-身份验证文件.该页面中列出了每个设置的功能.
如果您与从Google+获得的信息有关,我建议您定义一项功能:
def save_profile(backend, user, response, *args, **kwargs):
if backend.name == "google-oauth2":
# do something
Run Code Online (Sandbox Code Playgroud)
where user是一个django.contrib.auth.models.User对象,response是一个字典.然后将该函数添加到SOCIAL_AUTH_PIPELINE使用完整模块路径create_user.
如果您不想对该信息执行任何操作,则可以按原样保留默认管道.
最后,您需要将python-social-auth网址添加到您的网站urlpatterns:
from django.conf.urls import include
url("^soc/", include("social.apps.django_app.urls", namespace="social"))
Run Code Online (Sandbox Code Playgroud)
这应该做到!是时候进行测试了.首先,./manage.py makemigrations对于所需的迁移python-social-auth,然后./manage.py migrate,作为解释在这里.然后,您可以运行开发服务器,然后转到http:// localhost:8000/soc/login/google-oauth2 /?next = /.
希望我没有跳过解释任何步骤,它会工作.随意提出更多问题并阅读文档.此外,这是一个你应该检查的工作示例.
Geo*_*hev 42
@rhaps0dy的回答是正确的,但python-social-auth现在已被弃用并迁移为social-auth-app-django.所以这就是我与@rhaps0dy指南的不同之处.
python-social-auth,我安装social-auth-app-django,'social.apps.django_app.default' 变 'social_django''social.backends.google.GoogleOAuth2' 就是现在 'social_core.backends.google.GoogleOAuth2'url("^soc/", include("social.apps.django_app.urls", namespace="social")) 变 url("^soc/", include("social_django.urls", namespace="social"))| 归档时间: |
|
| 查看次数: |
16058 次 |
| 最近记录: |