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 account
But 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 touchPython
AndDjango
During this period, I found a lot of videos and materials to learn and practice.MVT structure
Baidu 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-auth
To achieve third-party login is very simple, convenient, direct and perfect, but has not been found
How can this problem be achieved? (this is not mentioned in the second article above .)social-auth
The Weixin content is not found in the explanation document, because the official website already has the correspondingUser
Data tables and third-party storageUserSocialAuth
The 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-auth
When I want to learn about the database storage methodsocial-backends
FoundWeixin.py
Is it supported?
Notes
Open Platform
You 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 domain
Enter the primary domain name of the website. for example, you cannot enterwww.zzmxy.com/login/wechat
You only need to writewww.zzmxy.com
(No need to add http or https). otherwiseThe redirect_uri parameter is incorrect.
!
Practical steps
Installsocial-auth
:
The official website usespython-social-auth==0.2.12
After the source code is downloadedsocial-backends
There areWeixin.py
To prove the availability;
pip install python-social-auth==0.2.12
social-auth
Configuration:
SOCIAL_AUTH_PIPELINE
Configuration: refer to the second article mentioned above;
AUTHENTICATION_BACKENDS
Configuration:
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 applicationAPPID
AndSECRET
Configuration:
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
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 permission
In actual operation, I found thatsocial-auth
The automatically generated QR code access link is missingscope
The 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,state
Can be omitted,scope
Is required, and authorized access to webpages,scope
The scope parameter is a fixed value.scope=snsapi_login
In this way, we needsocial-auth
In the source code, add this parameter value according to your actualsite-packages
Installation Path, find/social/backends/weixin.py
Files, suchVirtualEnv
The 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
Inparams
In the dictionary, addscope
The 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!