Website logon-python implementation, email logon-python implementation
We have recently logged on to open public beta. To make it easier for users, our product also decided to add the logon function, and then we will have this note.
Select the logon method as needed
Two logon access methods are available.
- Mobile Application Logon
- Website application Logon
Here we use website application login
Follow official process
1. Register and pass the open platform developer Qualification Certification
After registering an open platform account, enter the developer qualification authentication application in the account center and wait for the authentication to pass.
2. Create a website application
Create a website application by entering the website application name, Introduction, icon, and other information of each platform.
3 Access Logon
Check the website application development documentation in the resource center to develop the access login function so that users can log on to your website application.
If you have completed the above operations, continue to read it.
Website application logon is an OAuth2.0 authorized logon System Based on OAuth2.0 protocol standards.
OAuth2.0 authorized logon currently supports the authorization_code mode, which is suitable for applications with server-side authorization. The overall process of this mode is:
A third party initiates an authorized login request. After you authorize a third-party application, the application is pulled or redirected to a third-party website, and the authorization temporary ticket code parameter is included;
Add AppID and AppSecret to the code parameter, and exchange access_token through the API;
Use access_token to call the interface to obtain basic user data resources or help users perform basic operations.
For detailed procedures, refer to the official documentation. Here we only talk about the implementation method of python. Click here for the official document address
Refer to python-instagramI wrote a python-weixin (https://github.com/zongxiao/python-weixin) python SDK
But now onlyAccess and obtain user information, Refrefresh_tokenAnd other simple functions
First, clone the code to the local device.
Then execute
python setup.py install
Easy to use
1 from weixin. client import WeixinAPI 2 3 APP_ID = 'ur app id' 4 APP_SECRET = 'ur app secret' 5 REDIRECT_URI = 'HTTP: // your_domain.com/redirect_uri '# Be sure to add http/https 6 7 scope = ("snsapi_login",) 8 api = WeixinAPI (appid = APP_ID, 9 app_secret = APP_SECRET, 10 redirect_uri = REDIRECT_URI) 11 12 authorize_url = api. get_authorize_url (scope = scope)
Now
The authorize_url address is opened in the browser and will jump to the logon page.
Http: // your_domain.com/redirect_uri? Code = CODE & state = STATE page
Now we can use the code to obtain the logon access_token.
access_token = api.exchange_code_for_access_token(code=code)
The access_token information is
{ "access_token":"ACCESS_TOKEN", "expires_in":7200, "refresh_token":"REFRESH_TOKEN","openid":"OPENID", "scope":"SCOPE" }
Parameters |
Description |
Access_token |
Interface call credential (valid for currently 2 hours) |
Expires_in |
Access_token interface call credential timeout time, in seconds) |
Refresh_token |
User refresh access_token (valid for currently 30 days) |
Openid |
Unique Authorization User ID |
Scope |
User-authorized scopes, separated by commas (,) |
After obtaining the access_token, you can call the interface on the following prerequisites:
Access_token is valid and does not time out;
The user has authorized a third-party application account to apply the corresponding interface scope ).
For the interface scope, the interfaces that can be called include the following:
Scope) |
Interface |
Interface Description |
Snsapi_base |
/Sns/oau2/ access_token |
Use code in exchange for access_token, refresh_token, and authorized scope |
/Sns/oau2/ refresh_token |
Refresh or renew access_token |
/Sns/auth |
Check access_token Validity |
Snsapi_userinfo |
/Sns/userinfo |
Obtain user personal information |
Api = WeixinAPI (appid = APP_ID, app_secret = APP_SECRET, redirect_uri = REDIRECT_URI) # refresh or renew access_token using refresh_token = api. revoke (refresh_token = auth_info ['refresh _ token']) api = WeixinAPI (access_token = auth_info ['Access _ token']) # obtain user personal information user = api. user (openid = auth_info ['openid']) # check the validity of access_token v = api. validate_token (openid = auth_info ['openid'])
Now the login is complete.
The following is a complete example of using flask.
from flask import Flaskfrom flask import Markupfrom flask import redirectfrom flask import requestfrom flask import jsonifyfrom weixin.client import WeixinAPIfrom weixin.oauth2 import OAuth2AuthExchangeErrorapp = Flask(__name__)APP_ID = 'appid'APP_SECRET = 'app secret'REDIRECT_URI = 'http://localhost.com/authorization'@app.route("/authorization")def authorization(): code = request.args.get('code') api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI) auth_info = api.exchange_code_for_access_token(code=code) api = WeixinAPI(access_token=auth_info['access_token']) resp = api.user(openid=auth_info['openid']) return jsonify(resp)@app.route("/login")def login(): api = WeixinAPI(appid=APP_ID, app_secret=APP_SECRET, redirect_uri=REDIRECT_URI) redirect_uri = api.get_authorize_login_url(scope=("snsapi_login",)) return redirect(redirect_uri)@app.route("/")def hello(): return Markup('<a href="%s">weixin login!</a>') % '/login'if __name__ == "__main__": app.run(debug=True)
Reference link:
Website application access documentation
Website application creation address
Python-weixin github address https://github.com/zongxiao/python-weixin