How Django uses Social-Auth to scan third-party website QR codes on WeChat

Source: Internet
Author: User
Preface when we asked the webpage company to create a new official website, we planned to have a third-party account login function. However, due to the cumbersome application steps for some open platforms (especially open platforms) at that time, we had to delay, you can only add related functions by yourself recently. It's just getting started with Python and D... Preface

When we made the webpage company a new official websiteThird-party accountBut some of the open platform application steps at that time were cumbersome (especially the open platform), so it had been delayed. recently, only relevant functions could be added by myself.

Because it is just getting in touchPythonAndDjangoDuring this period, I found a lot of videos and materials to learn and practice.MVT structureBaidu found two useful articles on third-party login and learned a lot from them:

1. python implements third-party website QR code logon (Django)
2. use django-social-auth to log on to a Chinese social network website (QQ, Weibo, Douban, Baidu, Renren ,)

I deeply realized the use of QQ and Weibo login.social-authTo achieve third-party login is very simple, convenient, direct and perfect, but has not been foundHow can this problem be achieved? (this is not mentioned in the second article above .)social-authThe Weixin content is not found in the explanation document, because the official website already has the correspondingUserData tables and third-party storageUserSocialAuthThe data grid is very standard. after using the first method above to implement it, I am worried about adding and modifying user data tables. I really don't want to destroy that structure, so I just want to refresh it.social-authWhen I want to learn about the database storage methodsocial-backendsFoundWeixin.pyIs it supported?

Notes
  • Open PlatformYou need to submit a lot of certification materials to apply for and activate the service, and you also need to pay the annual certification fee of ¥300. it is different from the public number and service number. Address: http://open.weixin.qq.com

  • After the authentication is passed, add the corresponding web application. note:Authorization callback domainEnter the primary domain name of the website. for example, you cannot enterwww.zzmxy.com/login/wechatYou only need to writewww.zzmxy.com(No need to add http or https). otherwiseThe redirect_uri parameter is incorrect.!

Practical steps
  1. Installsocial-auth:
    The official website usespython-social-auth==0.2.12After the source code is downloadedsocial-backendsThere areWeixin.pyTo prove the availability;

    pip install python-social-auth==0.2.12
  2. social-authConfiguration:

    • SOCIAL_AUTH_PIPELINEConfiguration: refer to the second article mentioned above;

    • AUTHENTICATION_BACKENDSConfiguration:

      AUTHENTICATION_BACKENDS = ('social. backends. weibo. weibooau2', # Weibo's function 'social. backends. qq. qqoau2', # QQ function 'social. backends. weixin. weixinoau2', # This is the import function 'Oscar. apps. customer. auth_backends.EmailBackend ', 'Django. contrib. auth. backends. modelbackend ',)
    • Open platform applicationAPPIDAndSECRETConfiguration:

      SOCIAL_AUTH_WEIBO_KEY = '53 ***** 29 'social _ AUTH_WEIBO_SECRET = '2017 ************* 81a8b3 'social _ AUTH_QQ_KEY = '10 *** ** 51 'social _ AUTH_QQ_SECRET = '2017 ************* d15bd97 'social _ AUTH_WEIXIN_KEY = 'wx4fb ********** * 599 '# APPIDSOCIAL_AUTH_WEIXIN_SECRET = 'f1c17 ************ 08c0489' # SECRET of open platform applications
  3. After the configuration, run your website and access it with www. domain name. com/login/weixin to open the corresponding page. have you found any errors:Scope Parameter error or no Scope permissionIn actual operation, I found thatsocial-authThe automatically generated QR code access link is missingscopeThe official QR code access link is as follows:

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

    Five parameters are required,stateCan be omitted,scopeIs required, and authorized access to webpages,scopeThe scope parameter is a fixed value.scope=snsapi_loginIn this way, we needsocial-authIn the source code, add this parameter value according to your actualsite-packagesInstallation Path, find/social/backends/weixin.pyFiles, suchVirtualEnvThe created path is:

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

    Open this file and find it.def auth_params()This section (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

    InparamsIn the dictionary, addscopeThe parameters are 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 saving the modification, run the project again and access www. domain name. com/login/weixin again to see the effect!

The above describes how Django uses Social-Auth to scan for third-party websites. For more information, see other related articles in the first PHP community!

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.