Use Django-social-auth to do Chinese social networking website three-party login (QQ, Weibo, Douban)

Source: Internet
Author: User

The Django version for the requirements project is 1.4.8, the user system uses the Django default system, and already has tens of thousands of users, changing the Django User model or table, Django part do three-way login in Awesome-django recommended is also Django-social-auth, these two days trial, write a demo.

The code address only add the necessary requirements, the code configuration of the various app keys are a trial app (no audit), do not bother me ...

How to use Social_auth

Every website needs to register the app, this doesn't say

Here is an example of a Heroku app:http://llovebaimuda.herokuapp.com/

Note that the Social_auth configuration cannot be configured with callback addresses, so you must set the callback address on each major website to /你的域名/complete/weibo
Here is http://llovebaimuda.herokuapp.com/complete/weibo/ , of course, different sites are slightly different.

Commissioning Note! Local Settings hosts

Windows %systemroot%\system32\drivers\etc\hosts needs to find Notepad.exe in the Start menu, right-click to run as Administrator, and open this file to modify

Linux ' sudo vi/etc/hosts

To change the llovebaimuda.herokuapp.com to local or virtual machine IP, the development depends on him.

192.168.9.191 llovebaimuda.herokuapp.com

Individual site ConfigurationSina

http://open.weibo.com/webmaster, go to your app

Basic information on the left 网站信息 can see the domain name, app key, app secret

On 接口管理 授权机制 the left, locate OAuth2.0 授权设置 授权回调页 set tohttp://llovebaimuda.herokuapp.com/complete/weibo/

Qq

http://connect.qq.com/manage/index, go to the app you're applying to develop

You can see the app ID in the left Avatar, app key

Click on the information to edit, find 回调地址 , QQ callback as long as the domain name can be filled (no HTTP)llovebaimuda.herokuapp.com

Douban

http://developers.douban.com/apikey/, go to the app you're applying to develop

The overview section can see the API key and Secrect

Watercress does not need to fill in the callback address, but need to add test users, in the left 测试用户 section to add the user's watercress ID

Some configuration of Social_authSettings

Pipeline

Social_auth_pipeline = (    ' social.pipeline.social_auth.social_details ',    ' Social.pipeline.social_ Auth.social_uid ',    ' social.pipeline.social_auth.auth_allowed ',    ' Social_ Auth.backends.pipeline.social.social_auth_user ',    # User name associated with mailbox, document says there may be a problem    # ' Social_ Auth.backends.pipeline.associate.associate_by_email ',    ' social_auth.backends.pipeline.misc.save_status_to_ Session ',    ' social_auth.backends.pipeline.user.create_user ',    ' Social_ Auth.backends.pipeline.social.associate_user ',    ' social_auth.backends.pipeline.social.load_extra_data ', '    social_auth.backends.pipeline.user.update_user_details ',    ' social_auth.backends.pipeline.misc.save_ Status_to_session ',)

What kinds of OAuth do you want to use?

Authentication_backends = (    ' social_auth.backends.contrib.douban.Douban2Backend ',    # Note This is more special, Because Django-social-auth is dependent on Python-social-auth's    # python-social-auth==0.1.26, already contains the backend of QQ    # django-social-auth==0.8.1, it's not included.    # You need to add a file in Django-social-auth/social_auth/backends/contrib qq.py    # on one line    # from SOCIAL.BACKENDS.QQ import QQOAuth2 as Qqbackend    # then Setup is ok    ' Social_ Auth.backends.contrib.qq.QQBackend ',    ' social_auth.backends.contrib.weibo.WeiboBackend ',    # must be added, Otherwise, the Django Default user is not logged on    ' Django.contrib.auth.backends.ModelBackend ',

Some configuration of the template

Template_context_processors = (    ' Django.contrib.auth.context_processors.auth ',    # login can be used in the template "{% URL socialauth_begin ' douban-oauth2 '%} "    ' Social_auth.context_processors.social_auth_by_type_backends ',    ' Social_auth.context_processors.social_auth_login_redirect ',)

Various REDIRECT Connections

Social_auth_login_url = '/login-url/' social_auth_login_error_url = '/login-error/' Social_auth_login_redirect_url = ' /logged-in/' Social_auth_new_user_redirect_url = '/new-users-redirect-url/' social_auth_new_association_redirect_ URL = '/new-association-redirect-url/'

Various keys, secret

Social_auth_weibo_key = ' YOUR key ' Social_auth_weibo_secret = ' YOUR SECRET ' social_auth_qq_key = ' YOUR key ' social_auth_qq _secret = ' YOUR SECRET ' Social_auth_douban_oauth2_key = ' YOUR KEY ' Social_auth_douban_oauth2_secret = ' YOUR SECRET '
URLs
Urlpatterns = Patterns (",    ...    URL (r ', include (' Social_auth.urls ')),    ...)
Template

Pay attention to implement the method of/logout, with Django comes with

Login <li><a rel= "nofollow" href= "{% URL socialauth_begin ' Weibo '%}" >weibo</a></li><li> <a rel= "nofollow" href= "{% URL socialauth_begin ' qq '%}" >qq</a></li><li><a rel= "nofollow" href= "{% URL socialauth_begin ' douban-oauth2 '%}" >douban</a></li> logoff <a href= "/logout" > Logoff </a >
Trial

At this time the user table does not have any users, login method has WEIBO,DOUBAN,QQ

In [0]: User.objects.all () out[0]: []in [1]: UserSocialAuth.objects.all () out[1]: []

Open http://llovebaimuda.herokuapp.com/ it and don't forget to change the host.

Note or do not comment on the difference between this sentence ' Social_auth.backends.pipeline.associate.associate_by_email ',

First look without comments, my Weibo,qq,douban mailbox is a mailbox

Comment out Associate_by_email

Initial state

In [0]: User.objects.all () out[0]: []in [1]: UserSocialAuth.objects.all () out[1]: []

Log in with Weibo, redirect page tohttp://llovebaimuda.herokuapp.com/new-users-redirect-url/

In [2]: User.objects.all () out[2]: [<user: Aggressive 369>]in [3]: UserSocialAuth.objects.all () out[3]: [< Usersocialauth:usersocialauth Object>]

Click Logout, log in with QQ, redirect tohttp://llovebaimuda.herokuapp.com/new-association-redirect-url/

In [4]: User.objects.all () out[4]: [<user: Aggressive 369>]in [5]: UserSocialAuth.objects.all () out[5]: [< Usersocialauth:usersocialauth object> <usersocialauth:usersocialauth Object>]

Click Logout, log in with Douban, redirect tohttp://llovebaimuda.herokuapp.com/new-association-redirect-url/

In [6]: User.objects.all () out[6]: [<user: Aggressive 369>]in [7]: UserSocialAuth.objects.all () out[7]: [< Usersocialauth:usersocialauth object> <usersocialauth:usersocialauth Object>]

Clicking on any type of login without logging off will jump tohttp://llovebaimuda.herokuapp.com/new-association-redirect-url/

Click Sign Out, then click on any kind of login will jump tohttp://llovebaimuda.herokuapp.com/logged-in/

Open Associate_by_email

Initial state

In [0]: User.objects.all () out[0]: []in [1]: UserSocialAuth.objects.all () out[1]: []

Log in with Weibo, redirect page tohttp://llovebaimuda.herokuapp.com/new-users-redirect-url/

In [2]: User.objects.all () out[2]: [<user: Aggressive 369>]in [3]: UserSocialAuth.objects.all () out[3]: [< Usersocialauth:usersocialauth Object>]

Click Logout, log in with QQ, redirect to ' http://llovebaimuda.herokuapp.com/new-users-redirect-url/'

In [4]: User.objects.all () out[4]: [<user: Aggressive 349>, <user: Aggressive >]in [5]: UserSocialAuth.objects.all () out[5]: [& Lt Usersocialauth:usersocialauth object> <usersocialauth:usersocialauth Object>]

Click Logout, log in with Douban, redirect to ' http://llovebaimuda.herokuapp.com/new-users-redirect-url/'

In [4]: User.objects.all () out[4]: [<user: Aggressive 349>, <user: aggressive;, <user:43901973>]in [5]: UserSocialAuth.objects.all () out[5]: [<usersocialauth:usersocialauth object>, <usersocialauth: Usersocialauth object> <usersocialauth:usersocialauth Object>]

Click Sign out, log in any way (take QQ as an example), page jump tohttp://llovebaimuda.herokuapp.com/logged-in/

An exception occurs when logging in using either one of the 4 different login methods (in the case of watercress)

authalreadyassociated At/complete/douban-oauth2/this douban-oauth2 account was already in use.
The thinking of various redirects

SOCIAL_AUTH_LOGIN_URL = ‘/login-url/‘, I haven't found anything for the moment.

SOCIAL_AUTH_LOGIN_ERROR_URL = ‘/login-error/‘, login exception, should guide the user to re-login

SOCIAL_AUTH_LOGIN_REDIRECT_URL = ‘/logged-in/‘, successful login page

SOCIAL_AUTH_NEW_USER_REDIRECT_URL = ‘/new-users-redirect-url/‘, Django section New user, on this page can guide users to set up additional information such as Mailbox

SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = ‘/new-association-redirect-url/‘, the Django user has created an associated three-party account that does not come up with any special requirements that can be directly directed to the login Success page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.