微信公众号获取用户基本信息(UnionID机制)

首先参考微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/User_Management/Get_users_basic_information_UnionID.html#UinonId

  1. 我们可以通过access_token(是普通调用接口凭证,而非网页授权调用接口凭证)和openid(在自己的公众号配置中能找到)来获取用户基本信息。
    普通调用接口凭证获取方法请参照文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
    2.获取到access_token和openid和访问网址:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN就能返回一个带有用户基本信息的json数据
    代码如下:
import json
import requests
APP_ID = "wx7b10b0c5448d080b"
APP_SECRET = "4f57dcf9e1f866971f30931420b70955"
OPEN_ID = "oXazLs9rYeUCCMQt2fyOWBtPn2P0"

url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" %(APP_ID,APP_SECRET)

    response = requests.get(url)
    json_str = response.text
    resp_dict = json.loads(json_str)
    if "errcode" in resp_dict:
        # return HttpResponse("获取access_token失败")
        print("获取access_token失败")
    access_token1 = resp_dict.get("access_token")

url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN" %(access_token1,OPEN_ID)
response4 = requests.get(url)
json_str = response4.text
json_dict4 = json.loads(json_str)
print(json_dict4)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值