高级接口--用户标签管理

官方文档

官文中是标签管理。

创建标签:

def create_tag(access_token,name):
    # groups
    create_url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=%s'% access_token
#
create_url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token=%s'% access_token
# postData={'group':{"name":name}}

postData ={ "tag" : { "name" : name } }
    response = requests.post(create_url,json.dumps(postData,ensure_ascii=False).encode('utf-8'))
    return json.loads(response.text,encoding='utf-8')

获取公众号已创建的标签

def query_tags(access_token):
    query_url ='https://api.weixin.qq.com/cgi-bin/tags/get?access_token=%s'% access_token
    response = requests.get(query_url)
    return json.loads(response.text,encoding='utf-8'

编辑标签

def update_tag(access_toke,postData):
    '''{   "tag" : {     "id" : 134,     "name" : "广东人"   } } '''
    update_url='https://api.weixin.qq.com/cgi-bin/tags/update?access_token=%s'%access_token
    postData = json.dumps(postData,ensure_ascii=False).encode('utf-8')
    response = requests.post(update_url, postData)
    return json.loads(response.text,encoding='utf-8')

获取用户身上的标签列表

def query_usertags(access_token,openid):
    query_url='https://api.weixin.qq.com/cgi-bin/tags/getidlist?access_token=%s'% access_token
    postData ={
        'openid':openid
    }
    postData = json.dumps(postData, ensure_ascii=False).encode('utf-8')
    response = requests.post(query_url, postData)
    return json.loads(response.text,encoding='utf-8')

删除标签

def delete_tag(access_token,tag_id):
    delete_url ='https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=%s'% access_token
    postData =json.dumps({"tag":{ "id" : tag_id } },ensure_ascii=False).encode('utf-8')
    response = requests.post(delete_url, postData)
    return json.loads(response.text, encoding='utf-8')

批量给用户列表打标签

def batch_packagetag(access_token,openids,tag_id):
    package_url ='https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=%s'% access_token
    postData ={
    "openid_list" :openids,
    "tagid" : tag_id
 }
    postData = json.dumps(postData)
    response = requests.post(package_url, postData)
    return json.loads(response.text, encoding='utf-8')

批量取消用户列表标签

def batch_untag(accesss_token,openids,tag_id):
    untag_url = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=%s' % access_token
    postData = {
        "openid_list": openids,
        "tagid": tag_id
    }
    postData = json.dumps(postData)
    response = requests.post(untag_url, postData)
    return json.loads(response.text, encoding='utf-8')

获取标签下的粉丝列表:

def get_tag_fans(access_token,tag_id,next_openid=''):
    #获取标签吓粉丝列表
    query_url ='https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token=%s'% access_token
    params ={
        "tagid": tag_id, "next_openid": next_openid
    }
    response = requests.post(query_url,params)
    return json.loads(response.text,encoding='utf-8')
注意:这个函数有问题。没有任何用户的标签,竟然返回了用户。需要在正式公众号下面测试。另外文档说是GET,实际需要post

猜你喜欢

转载自www.cnblogs.com/ahMay/p/12074744.html