django 认证_Django中的LinkedIn社会认证

django 认证

Social Authentication (or Social Login) is a way to simplify logins for end users by using existing login information from the popular social networking services such as Facebook, Twitter, Google+, LinkedIn (focus of this article), and so on. Most websites that require a user to login, utilize social login platforms for a better authentication/registeration experience in lieu of developing their own systems.

社交身份验证(或社交登录)是一种通过使用来自流行的社交网络服务(例如FacebookTwitterGoogle+LinkedIn (本文重点)等)的现有登录信息来简化最终用户登录的方法。 大多数要求用户登录的网站都使用社交登录平台来获得更好的身份验证/注册体验,而不是开发自己的系统。

Python Social Auth provides a mechanism for easily setting up an authentication/registration system, which supports several frameworks and auth providers. In this tutorial, we’ll demonstrate in detail how to integrate this library into your Django Project to provide user authentication through LinkedIn using OAuth 2.0.

Python Social Auth提供了一种轻松设置身份验证/注册系统的机制,该系统支持多个框架和auth提供程序。 在本教程中,我们将详细演示如何将该库集成到Django项目中,以使用OAuth 2.0通过LinkedIn提供用户身份验证。

django-linkedin

什么是OAuth 2.0? (What is OAuth 2.0?)

OAuth 2.0 is the authorization framework which allows applications to gain access to an end user’s account for authenticating/registering via popular social networking services. The end user gets to choose which details the application has access to. It focuses on simplifying the development workflow while providing specific authorization flows for web applications and desktop applications, mobile phones, and IOT (Internet of Things) devices.

OAuth 2.0是授权框架,允许应用程序通过流行的社交网络服务访问最终用户的帐户以进行身份​​验证/注册。 最终用户可以选择应用程序可以访问的详细信息。 它着重于简化开发工作流程,同时为Web应用程序和桌面应用程序,移动电话以及IOT(物联网)设备提供特定的授权流程。

环境和Django设置 (Environment and Django Set up)

We’ll be using:

我们将使用:

  • python v3.4.2
  • Django v1.8.4
  • python-social-auth v0.2.12
  • python v3.4.2
  • Django v1.8.4
  • python-social-auth v0.2.12

If you already have a virtualenv and a Django Project set up and ready to go, feel free to skip this section.

如果您已经设置了virtualenv和Django Project并准备就绪,请随时跳过此部分。

创建一个virtualenv (Create a virtualenv)

1
1
2
2

开始一个新的Django项目 (Start a new Django Project)

Bootstrap a Django Application:

引导Django应用程序:

1
1
2
2
3
3

Don’t forget to add the app to the INSTALLED_APPS tuple in settings.py, for our Project to know that we have created an app that needs to be served as a part of our Django Project.

不要忘记将应用程序添加到settings.py中的INSTALLED_APPS元组中,以便我们的项目知道我们已经创建了一个应用程序,该应用程序需要作为Django项目的一部分。

设置初始表 (Set up the initial tables)

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20

添加超级用户 (Add a superuser)

1
1

创建模板目录 (Create a templates directory)

Create a new directory called “templates” in the project root, and then add the correct path to the TEMPLATES in the settings.py file:

在项目根目录中创建一个名为“ templates”的新目录,然后在settings.py文件中向TEMPLATES添加正确的路径:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15

运行健全性检查 (Run a sanity check)

Fire up the development server – python manage.py runserver to ensure all is in order and then navigate to http://127.0.0.1:8000/. You should see the “It worked!” page.

启动开发服务器– python manage.py runserver以确保一切正常,然后导航到http://127.0.0.1:8000/ 。 您应该看到“有效”! 页。

Your project should look like this:

您的项目应如下所示:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17

Python社交身份验证设置 (Python Social Auth Set up)

Follow the steps below and/or the official installation guide to install and set up the basic configuration in order to make our app handle the social login via any social networking service.

请按照以下步骤和/或官方安装指南安装和设置基本配置,以使我们的应用通过任何社交网络服务处理社交登录。

安装 (Installation)

Install with pip:

用pip安装:

1
1

组态 (Configuration)

Update settings.py to include/register the library in our project:

更新settings.py以在我们的项目中包含/注册该库:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
32
32

NOTE: Since we’re using LinkedIn Social Authentication, we need the Linkedin OAuth2 backend:

注意 :由于我们使用的是LinkedIn社会认证,因此我们需要Linkedin OAuth2后端:

1
1
2
2
3
3
4
4

运行迁移 (Run a migration)

Once registered, update the database:

注册后,更新数据库:

1
1
2
2

更新网址 (Update the URLs)

Within the Project’s urls.py file, update the urlpatters to inclue the main auth URLs:

在项目的urls.py文件中,更新urlpatters以包含主要的身份验证URL:

1
1
2
2
3
3
4
4

Next we need to grab the required authentication keys from a LinkedIn Application. This process is similar for many popular social networks – like Twitter, Facebook and Google.

接下来,我们需要从LinkedIn应用程序中获取所需的身份验证密钥。 对于许多流行的社交网络(例如Twitter,Facebook和Google)来说,此过程都是相似的。

LinkedIn身份验证密钥 (LinkedIn Authentication Keys)

In order for our app to distinctly recognize the LinkedIn social login that we implemented, we need to have some app-specific credentials to differentiate our app’s login from other social logins on the web.

为了使我们的应用能够清楚地识别我们实施的LinkedIn社交登录,我们需要具有一些特定于应用的凭据,以区分我们的应用登录和网络上的其他社交登录。

Create a new application at https://www.linkedin.com/developer/apps and make sure to use a callback/redirect URL of http://127.0.0.1:8000/complete/linkedin-oauth2/ (very important!). This URL is specific to OAuth 2.0, remember that.

https://www.linkedin.com/developer/apps上创建一个新应用程序,并确保使用http://127.0.0.1:8000/complete/linkedin-oauth2/的回调/重定向URL(非常重要!) 。 该网址特定于OAuth 2.0,请记住。

NOTE: The callback URL used above is valid only in local development and will need be changed when you move to a production or staging environment.

注意 :上面使用的回调URL仅在本地开发中有效,并且在移至生产或暂存环境时需要进行更改。

In the “django_social_project” directory, add a new file called config.py. Then grab the Consumer Key (API Key) and the Consumer Secret (API Secret) from LinkedIn and add them to the file:

在“ django_social_project”目录中,添加一个名为config.py的新文件。 然后从领英中获取使用者密钥(API密钥)和使用者密钥(API密钥),并将它们添加到文件中:

1
1
2
2

Let’s also add the following URLs to the config.py file to specify the login and the redirect URLs (after a user authenticates):

我们还将以下URL添加到config.py文件中,以指定登录名和重定向URL(在用户进行身份验证之后):

1
1
2
2

Add the following imports to settings.py

将以下导入添加到settings.py

1
1

In order for our app to complete the login process successfully we need to define the Templates and Views associated with those URLs. Let’s do that now.

为了使我们的应用程序成功完成登录过程,我们需要定义与这些URL关联的模板和视图。 现在开始吧。

友好的意见 (Friendly Views)

For checking whether our app is working or not we just need two views – login and home.

为了检查我们的应用程序是否正常运行,我们仅需要两个视图-登录名和首页。

网址 (URLs)

Update the urlpatterns in the Project’s urls.py file, to map our URLs to our views that we’ll be seeing in the further sections:

更新项目的urls.py文件中的urlpatterns,以将我们的URL映射到我们在其他部分中将看到的视图:

1
1
2
2
3
3
4
4
5
5
6
6
7
7

观看次数 (Views)

Now, add the views to the App’s views.py for making our routes know what exactly to do when a particular route is hit.

现在,将视图添加到应用程序的views.py中,以使我们的路线知道遇到特定路线时的确切操作。

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18

So, in the login function we fetched the logged-in user using the RequestContext.

因此,在登录功能中,我们使用RequestContext获取了登录用户。

范本 (Templates)

Add two templates to the “templates” folder – login.html:

将两个模板添加到“模板”文件夹– login.html:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9

And home.html:

还有home.html:

1
1
2
2
3
3
4
4
5
5

Your project should now look like this:

您的项目现在应如下所示:

1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20

测试! (Test!)

Now just fire up the server again to test:

现在只需再次启动服务器进行测试:

1
1

翻译自: https://www.pybloggers.com/2015/10/linkedin-social-authentication-in-django/

django 认证

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值