pip install rauth
python
import rauth
import webbrowser
from flask import Flask, request
python
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['CALLBACK_URL'] = 'http://localhost:5000/callback'
python
def wechat_login():
oauth = rauth.OAuth2Service(
client_id='your_client_id',
client_secret='your_client_secret',
name='wechat',
access_token_url='https://api.weixin.qq.com/sns/oauth2/access_token',
authorize_url='https://open.weixin.qq.com/connect/qrconnect',
base_url='https://api.weixin.qq.com/',
)
params = {'redirect_uri': app.config['CALLBACK_URL'], 'response_type': 'code'}
authorize_url = oauth.get_authorize_url(**params)
webbrowser.open(authorize_url)
@app.route('/callback')
def callback():
code = request.args.get('code')
data = {
'appid': 'your_appid',
'secret': 'your_app_secret',
'code': code,
'grant_type': 'authorization_code',
}
response = rauth.post('https://api.weixin.qq.com/sns/oauth2/access_token', data=data)
access_token = response.json().get('access_token')
user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token={}&openid={}'.format(access_token, 'your_openid')
response = rauth.get(user_info_url)
user_info = response.json()
app.run()