search
HomeBackend DevelopmentPython TutorialIntroduction to how Django uses Social-Auth to implement QR code login on WeChat third-party websites

Preface

When we asked the web company to create a new official website, we planned to have a third-party account login function, but due to the relatively complex application procedures of some open platforms at that time It is cumbersome (especially the WeChat open platform), so I have been procrastinating it. Recently, I can only add related functions myself.

Since I am new to <a href="http://www.php.cn/wiki/1514.html" target="_blank">Python</a> and <a href="http://www.php.cn/wiki/1515.html" target="_blank">Django</a>, I found a lot of videos and After studying the materials and practicing , I slowly figured out the MVT structure and so on. Baidu found two very useful articles on third-party login, and I learned a lot from them:

1. Python implements QR code scanning login for third-party websites on WeChat (Django)
2. Use django-social-auth for three-party login on Chinese social networking sites (QQ, Weibo, Douban, Baidu, Renren, WeChat)

When I implemented QQ and Weibo login, I deeply realized that using social-auth to achieve third-party login is very simple, convenient, direct and perfect, but I have never found it# How can ##微信 be implemented in the same way (the second article above also did not mention it); from the interpretation document of social-auth, I couldn’t find the content of Weixin, because the official website has There is a corresponding User data table and a third-party UserSocialAuth data grid, which is very standardized. After using the first method above, I was troubled by adding and modifying the user data table. I really didn’t want to destroy that structure, so when I picked up social-auth and wanted to learn how to store databases, I discovered it in social-backend<a href="http://www.php.cn/wiki/1048.html" target="_blank">s</a> Weixin.py, doesn’t that mean it can support WeChat?

Notes

  • WeChat Open Platform Application and activation require submission of a lot of certification information. You also need to pay an annual certification fee of RMB300; it is different from official accounts, service accounts, etc. Address: http://open.weixin.qq.com

  • After passing the authentication, add the corresponding web application, pay attention to filling in the

    authorization callback domain, and write the website The main domain name is enough. For example, you cannot write www.zzmxy.com/login/wechat. You only need to write www.zzmxy.com (no need to add http Or https), otherwise the subsequent periods will all be redir<a href="http://www.php.cn/wiki/1275.html" target="_blank">ect_uri parameter errors</a>!

Practical steps

  1. Installationsocial-authSince the official website uses
    python-social-auth==0.2.12 , after downloading the source code, I found that there is also Weixin.py in social-backends, which proves that it is available;

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

  • SOCIAL_AUTH_PIPELINEConfiguration: 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.weixin.WeixinOAuth2',    #这个是导入微信的功能
       'oscar.apps.customer.auth_backends.EmailBackend',
       'django.contrib.auth.backends.ModelBackend',
       )
  • Configuration of WeChat Open Platform Application

    APPID and SECRET

    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'            #开放平台应用的APPID
    SOCIAL_AUTH_WEIXIN_SECRET = 'f1c17************08c0489'    #开放平台应用的SECRET
  • After configuring, run your website and use www.domain name.com/login/weixin to access the corresponding page to open the corresponding page, but have you noticed any problems? Error:

    Scope parameters are wrong or there is no Scope permission. During the actual operation, I found that social-auth is missing a in the automatically generated QR code access link. scope parameter, and the QR code access link provided by WeChat 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
    The above required parameters are 5,

    state can be omitted, but scope is required, and for authorized access to web pages, the scope scope parameter is a fixed value scope=snsapi<a href="http://www.php.cn/js/js-weixinapp-api.html" target="_blank">_login</a>. In this case, we You need to add this parameter value to the source code of social-auth, and find /social/backends/ according to your actual site-packages installation path. weixin.py file, for example, the path I created using VirtualEnv is:

    /home/ubuntu/env/mppython/lib/python2.7/site-packages/social/backends/weixin.py
    Open this file and find the content inside

    def auth_params() ( Original text):

    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, just add a scope parameter, and the modification is 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 modification and saving, then Rerun the project and visit www.domain name.com/login/weixin again to see the effect!

  • The above is the detailed content of Introduction to how Django uses Social-Auth to implement QR code login on WeChat third-party websites. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

    详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

    Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

    归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

    分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

    VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

    python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

    pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

    Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

    详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

    本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft