Django uses Social-auth to implement WeChat third-party website scanning Code login method Introduction

Source: Internet
Author: User

Objective

Before the web company to create a new network when planning 第三方账号 the login function, but due to some of the open platform application steps are cumbersome (especially open platform), so has been delayed, to the recent can only add related features.

Because it is just contact Python and Django , during the period to find a lot of video and information learning practice, only slowly to MVT结构 figure out what to understand, third-party login Baidu found two very useful articles, also learned from a lot:

1. Python for third-party website scan Login (Django)
2. Use Django-social-auth to do Chinese social networking website three-party login (QQ, Weibo, watercress, Baidu, everyone,)

In the implementation of QQ, micro-blog on the deep experience to use social-auth to achieve third-party login is very simple and convenient, direct and perfect, but has not been found how also to achieve (the social-auth second article above is not mentioned); Interpretation of the document also Leng did not find the content of Weixin, because the official website has a corresponding User data sheet and storage of third-party UserSocialAuth data, very standardized, in the use of the first method implemented after the user is distressed by the addition and modification of the data table, really do not want to destroy that structure, is to regain social-authwant to learn how to store the database when, unexpectedly social-backends found in Weixin.py , that does not explain can support it?

Precautions

    • 开放平台Application and opening of the need to submit a lot of certification materials, but also to pay ¥300 annual certification fees, and the public number, service number, etc. not the same OH. Address: http://open.weixin.qq.com

    • After the certification, add the appropriate Web application, note that 授权回调域 the main domain name of the site can be written, such as can not write www.zzmxy.com/login/wechat , just to write www.zzmxy.com (do not need to add HTTP or HTTPS), or later redirect_uri 参数错误 !

Real-Operating steps

  1. Installation social-auth :
    Due to the use of the official website python-social-auth==0.2.12 , the download source back, found in social-backends there is also Weixin.py , proof available;

    Pip Install python-social-auth==0.2.12
  2. social-authConfiguration:

      • social_auth_pipeline configuration: Please refer to the writing of the second article mentioned above;

      • authentication_backends configuration:

        authentication_backends = (' Social.backends.weibo.WeiboOAuth2 ', #微博的功能 ' social.backends.qq.QQOAuth2 ', #QQ的功能 ' Social.backends.weix In. WeixinOAuth2 ', #这个是导入的功能 ' oscar.apps.customer.auth_backends. Emailbackend ', ' django.contrib.auth.backends.ModelBackend ',) 
      • Open Platform Application APPID and SECRET configuration:

        social_auth_weibo_key = ' 53*****29 ' social_auth_weibo_ SECRET = ' 272152************81a8b3 ' social_auth_qq_key = ' 10*****51 ' Social_auth_qq_secret = ' 5807************ D15bd97 ' Social_auth_weixin_key = ' wx4fb***********599 ' #开放平台应用的APPIDSOCIAL_AUTH_WEIXIN_SECRET = ' f1c17********* 08c0489 ' #开放平台应用的SECRET 
  3. After configuring, run your website, use www. com/login/weixin access to open the corresponding page, but there is no error found: Scope参数错误或没有Scope权限 , in the actual operation, I found social-auth the automatic generation of QR code access link, is less a scope Parameters, and the official QR code access link is this:

    Https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code &scope=scope&state=state#wechat_redirect

    If the required parameters are 5, state can be omitted, but scope is required, and for Web page authorization access, the scope scope parameter is a fixed value scope=snsapi_login , so we need to social-auth the source code, this parameter value to add, according to your actual site-packages installation path To locate /social/backends/weixin.py the file, as I used to VirtualEnv build the path is:

    /home/ubuntu/env/mppython/lib/python2.7/site-packages/social/backends/weixin.py

    Open this file and find def auth_params() the contents of this paragraph (original):

    def auth_params (self, state=none):    AppID, secret = Self.get_key_and_secret ()    params = {        ' AppID ': AppID,        ' Redirect_uri ': Self.get_redirect_uri (state),    }    If self. State_parameter and state:        params[' state ') = State    if self. Response_type:        params[' response_type '] = self. Response_type    return params

    In the params dictionary, add a scope parameter and modify it as follows:

    def auth_params (self, state=none):    AppID, secret = Self.get_key_and_secret ()    params = {        ' AppID ': AppID,        ' Redirect_uri ': Self.get_redirect_uri (state),        ' scope ': ' Snsapi_login ',    }    if self. State_parameter and state:        params[' state ') = State    if self. Response_type:        params[' response_type '] = self. Response_type    return params

    After you save, re-run the project and revisit www. domain name. Com/login/weixin can see the effect!

Related Article

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.