Python教程

python实现微信公众平台网页授权登录获取用户基本信息

本文主要是介绍python实现微信公众平台网页授权登录获取用户基本信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

from urllib import parse
import requests

获取access_token

appid = "wxa77a00333cd8d99a"
secret = "6f61xxxxxxxxxx"
get_access_token_url = f"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}"

access_token = requests.get(get_access_token_url).json().get("access_token")

获取微信服务器ip列表

get_server_ip_list_url = f"https://api.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token={access_token}"
server_ip_list = requests.get(get_server_ip_list_url).json()
print("server_ip_list:", server_ip_list["ip_list"])

获取微信callback IP地址

callback_url = f"https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token={access_token}"
callback_ip = requests.get(callback_url).json()
print("callback_ip_list:", callback_ip["ip_list"])

查询rid信息

rid_url = f"https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={access_token}"
rid_ret = requests.post(rid_url, data={"rid": "6260f2e7-2caa4faa-752c531"})
print(rid_ret.text)

scope = "snsapi_userinfo"
scope = "snsapi_base"
redirect_uri = parse.quote("http://192.168.110.10:8000")
url = f"https://open.weixin.qq.com/connect/oauth2/authorize?appid={appid}&redirect_uri={redirect_uri}&response_type=code&scope={scope}&state=STATE#wechat_redirect"
print(url)

import json
from flask import Flask, request

app = Flask(__name__)


@app.route(rule="/")
def index():
    ### 1. 获取code
    code = request.args.get("code")
    state = request.args.get("state")
    print(code, state)

    ### 2. 通过code换取网页授权access_token和用户openid
    """
    获取code后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
    """
    url = f"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code"
    access_token_openid_dict = requests.get(url).json()
    print(access_token_openid_dict)
    access_token = access_token_openid_dict["access_token"]
    refresh_token = access_token_openid_dict["refresh_token"]
    openid = access_token_openid_dict["openid"]

    ### 3. 刷新access_token(如果需要)
    """
    获取第二步的refresh_token后,请求以下链接获取access_token: https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
    """
    url = f"https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={appid}&grant_type=refresh_token&refresh_token={refresh_token}"
    ret = requests.get(url).json()
    print(ret)

    ##3 4. 拉取用户信息(需scope为 snsapi_userinfo)
    """
    http:GET(请使用https协议) https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
    """
    url = f"https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}&lang=zh_CN"
    ret = requests.get(url).text
    import json
    ret = json.loads(ret)
    print(ret)
    data = json.loads(ret.text.encode("ISO-8859-1").decode("utf-8"))  # 解决微信编码不一致问题
    print(data)

    ### 附:检验授权凭证(access_token)是否有效
    """
    http:GET(请使用https协议) https://api.weixin.qq.com/sns/auth?access_token=ACCESS_TOKEN&openid=OPENID
    """
    url = f"https://api.weixin.qq.com/sns/auth?access_token={access_token}&openid={openid}"
    ret2 = requests.get(url).json()
    print(ret2)

    return ret


if __name__ == '__main__':
    app.run(host="192.168.110.10", port="8000", debug=True)
这篇关于python实现微信公众平台网页授权登录获取用户基本信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!