如何使用Django登入Google+ API? [英] How to sign in with the Google+ API using Django?
问题描述
如何添加Google+ API登录到Django网站?
How might I go about adding a Google+ API sign-in to my Django website?
推荐答案
首先您必须创建OAuth Google +的凭据。
First you must create OAuth credentials for Google+.
- 转到 Google开发者控制台
- 创建一个新项目。
- 转到API和验证 - >授权屏幕并给你的产品一个名字。点击保存。
- 转到API和身份验证 - >凭据。在OAuth下,单击创建新客户端ID。添加 http:// localhost:8000 / soc / complete / google-oauth2 / 应该被列为回调URL。这只适用于测试,确保在生产中放入您的实际域。
- Go to the Google Developer Console
- Create a new project.
- Go to "APIs and authentication" -> "Authorization screen" and give your product a name. Click "Save".
- 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.
- 安装
python-social -auth
与pip
-
设置适当的Django设置:
- Install
python-social-auth
withpip
Set the appropriate Django settings:
- 将
'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'
。一个例子是:
- Add
'social.apps.django_app.default'
toINSTALLED_APPS
: - Add the
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY
andSOCIAL_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')
定义 SOCIAL_AUTH_PIPELINE
设置,详见 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
其中用户
是一个 django.contrib.auth.models.User
对象, / code>是一个字典。然后在
create_user
之后,使用完整模块路径将该功能添加到 SOCIAL_AUTH_PIPELINE
。
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
urls添加到您网站的 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
,如 here 。然后,您可以运行开发服务器,然后转到 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屋!