基于python 微信小程序之獲取已存在模板消息列表
前言:
為了獲取一定高級(jí)操作,如:微信模板消息(xiao,xin)推送,把消息推送給用戶,或者是獲取用戶授權(quán)信息都需要用到access token,有效期為兩個(gè)小時(shí)?
過(guò)了兩個(gè)小時(shí)怎么辦?重新獲取,來(lái),代碼擼起走,啥女朋友沒(méi)有?
獲取小程序?qū)?yīng)的access token
def get_wx_token(): url = "https://api.weixin.qq.com/cgi-bin/token?" try: respone = requests.get(url, params=payload, timeout=50) access_token = respone.json().get("access_token") res = respone.json() res["time"] = stamp print(u'token過(guò)期,重新寫(xiě)入文件的內(nèi)容>>>', res) with open(token_file, "w+") as f: f.write(json.dumps(res)) return access_token except Exception as e: msg = traceback.format_exc() print('get token error', msg) return
并判斷是否過(guò)期
def get_access_token(): try: with open(token_file, "r") as f: content = f.read() data_dict = content # 如果緩存內(nèi)容為空,直接重新獲取token if (content == ''): print("token文件為空,重新獲取并寫(xiě)入文件") result = get_wx_token() return result else: data_dict = re.sub('\'', '\"', data_dict) token_time = int(json.loads(data_dict)['time']) if (stamp - token_time) > 7100: # print("token過(guò)期,重新獲取并寫(xiě)入文件") get_wx_token() else: return json.loads(data_dict)['access_token'] except Exception as e: msg = traceback.format_exc() print("access token express time", msg)
根據(jù)access token 獲取模板列表
def get_templates_list(access_token): url = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token={}".format(access_token) data = { "offset": 0, "count": 20 } r = requests.post(url,data=json.dumps(data)).json() tpl_list = r.get('list') for tpl in tpl_list: print(tpl) # print(r.get('list'))
返回?cái)?shù)據(jù)示例
{ "errcode": 0, "errmsg": "ok", "list": [ { "template_id": "wDYzYZVxobJivW9oMpSCpuvACOfJXQIoKUm0PY397Tc", "title": "購(gòu)買(mǎi)成功通知", "content": "購(gòu)買(mǎi)地點(diǎn){{keyword1.DATA}}\n購(gòu)買(mǎi)時(shí)間{{keyword2.DATA}}\n物品名稱{{keyword3.DATA}}\n", "example": "購(gòu)買(mǎi)地點(diǎn):TIT造艦廠\n購(gòu)買(mǎi)時(shí)間:2016年6月6日\(chéng)n物品名稱:咖啡\n" } ] }
總結(jié)
以上所述是小編給大家介紹的基于python 微信小程序之獲取已存在模板消息列表,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python實(shí)現(xiàn)協(xié)程的具體示例
協(xié)程是一種輕量級(jí)的并發(fā)編程技術(shù),它允許程序在某個(gè)點(diǎn)上暫停執(zhí)行,本文主要介紹了python實(shí)現(xiàn)協(xié)程的具體示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03python 實(shí)現(xiàn)多維數(shù)組轉(zhuǎn)向量
今天小編就為大家分享一篇python 實(shí)現(xiàn)多維數(shù)組轉(zhuǎn)向量,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11解決安裝python庫(kù)時(shí)windows error5 報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決安裝python庫(kù)時(shí)windows error5 報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python爬取用戶觀影數(shù)據(jù)并分析用戶與電影之間的隱藏信息!
看電影前很多人都喜歡去 『豆瓣』 看影評(píng),所以我爬取44130條 『豆瓣』 的用戶觀影數(shù)據(jù),分析用戶之間的關(guān)系,電影之間的聯(lián)系,以及用戶和電影之間的隱藏關(guān)系,需要的朋友可以參考下2021-06-06Python使用socket實(shí)現(xiàn)組播與發(fā)送二進(jìn)制數(shù)據(jù)
在工作中經(jīng)常會(huì)用到socket傳輸數(shù)據(jù),例如客戶端給服務(wù)器發(fā)送數(shù)據(jù)(雙方約定了數(shù)據(jù)格式),本文主要介紹了Python使用socket實(shí)現(xiàn)組播與發(fā)送二進(jìn)制數(shù)據(jù),感興趣的可以了解一下2021-06-06淺談python中scipy.misc.logsumexp函數(shù)的運(yùn)用場(chǎng)景
下面小編就為大家?guī)?lái)一篇淺談python中scipy.misc.logsumexp函數(shù)的運(yùn)用場(chǎng)景。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06Python實(shí)現(xiàn)過(guò)濾單個(gè)Android程序日志腳本分享
這篇文章主要介紹了Python實(shí)現(xiàn)過(guò)濾單個(gè)Android程序日志腳本分享,本文講解了原理、實(shí)現(xiàn)代碼、使用方法、最新代碼等內(nèi)容,需要的朋友可以參考下2015-01-01