微信公众号获取用户基本信息(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)

你可能感兴趣的:(微信公众号获取用户基本信息(UnionID机制))