如何使用 Django 使用 Google+ API 登录? [英] How to sign in with the Google+ API using Django?

查看:31
本文介绍了如何使用 Django 使用 Google+ API 登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Google+ API 登录添加到我的 Django 网站?

How might I go about adding a Google+ API sign-in to my Django website?

推荐答案

首先,您必须为 Google+ 创建 OAuth 凭据.

First you must create OAuth credentials for Google+.

  1. 转到 Google 开发者控制台
  2. 创建一个新项目.
  3. 转到API 和身份验证"->授权屏幕"并为您的产品命名.点击保存".
  4. 转到API 和身份验证"->凭据".在OAuth"下,单击创建新客户端 ID".添加 "http://localhost:8000/soc/complete/google-oauth2/" 应列为回调 URL.这仅适用于测试,请确保在生产时放入您的实际域.
  1. Go to the Google Developer Console
  2. Create a new project.
  3. Go to "APIs and authentication" -> "Authorization screen" and give your product a name. Click "Save".
  4. Go to "APIs and authentication" -> "Credentials". Under "OAuth", click "Create New Client ID". Add "http://localhost:8000/soc/complete/google-oauth2/" should be listed as a callback URL. This will only work for testing, make sure to put in your actual domain when in production.

现在让我们将 python-social-auth 添加到您的 Django 应用中.

Now let's add python-social-auth to your Django app.

  1. 使用 pip
  2. 安装 python-social-auth
  3. 设置适当的 Django 设置:

  1. Install python-social-auth with pip
  2. Set the appropriate Django settings:

  • 'social.apps.django_app.default' 添加到 INSTALLED_APPS:
  • 使用您之前创建的客户端密钥和机密添加 SOCIAL_AUTH_GOOGLE_OAUTH2_KEYSOCIAL_AUTH_GOOGLE_OAUTH2_SECRET 设置.客户端密钥是 Google 开发者控制台凭据"屏幕中列出的客户端 ID",以.apps.googleusercontent.com"结尾.只取点之前的部分.该机密被列为客户端机密".
  • 确保您明确定义了 AUTHENTICATION_BACKENDS 设置,并且它包含 'social.backends.google.GoogleOAuth2'.一个例子是:

  • Add 'social.apps.django_app.default' to INSTALLED_APPS:
  • Add the SOCIAL_AUTH_GOOGLE_OAUTH2_KEY and SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET settings with the client key and secret you created earlier. The client key is the "Client ID" listed in the "Credentials" screen in the Google developer console, the one which ends in ".apps.googleusercontent.com". Only take the part before the dot. The secret is listed as "Client secret".
  • Make sure you have the AUTHENTICATION_BACKENDS setting explicitly defined, and that it contains 'social.backends.google.GoogleOAuth2'. An example would be:

AUTHENTICATION_BACKENDS = (
    'social.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend')

  • 按照 python-social-auth 文档.该页面中列出了每个设置的作用.

  • Define the SOCIAL_AUTH_PIPELINE setting as detailed in the python-social-auth documentation. What every setting does is listed in that page.

    如果您想处理从 Google+ 获得的信息,我建议您定义一个函数:

    If you have something to do with the information you get from Google+, I recommend defining a function:

        def save_profile(backend, user, response, *args, **kwargs):
            if backend.name == "google-oauth2":
               # do something
    

    其中 user 是一个 django.contrib.auth.models.User 对象,而 response 是一个字典.然后使用完整的模块路径将该函数添加到 SOCIAL_AUTH_PIPELINE,在 create_user 之后.

    where user is a django.contrib.auth.models.User object, and response is a dictionary. Then add that function to the SOCIAL_AUTH_PIPELINE using the full module path, after create_user.

    如果您不想对这些信息做任何事情,您可以保持默认管道不变.

    If you don't want to do anything with that information you can leave the default pipeline as-is.

    最后,您需要将 python-social-auth 网址添加到您网站的 urlpatterns:

    Finally, you'll want to add the python-social-auth urls to your site's urlpatterns:

    from django.conf.urls import include 
    url("^soc/", include("social.apps.django_app.urls", namespace="social"))
    

    那应该可以!是时候进行测试了.首先,./manage.py makemigrations 用于python-social-auth 所需的迁移,然后./manage.py migrate,如在此处进行了解释.然后就可以运行开发服务器了,进入http://localhost:8000/soc/login/google-oauth2/?next=/ .

    And that should do it! It's time for testing. First, ./manage.py makemigrations for the required migrations of python-social-auth, and then ./manage.py migrate, as explained here. Then, you can run the development server, and go to http://localhost:8000/soc/login/google-oauth2/?next=/ .

    希望我没有跳过解释任何步骤,它会起作用.欢迎提出更多问题并阅读文档.另外,这里有一个工作示例,您应该检查一下出来.

    Hopefully I did not skip explaining any step and it will work. Feel free to ask more questions and read the docs. Also, here is a working example that you should check out.

    这篇关于如何使用 Django 使用 Google+ API 登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆