基于python 微信小程序之獲取已存在模板消息列表
前言:
為了獲取一定高級(jí)操作,如:微信模板消息(xiao,xin)推送,把消息推送給用戶(hù),或者是獲取用戶(hù)授權(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物品名稱(chēng){{keyword3.DATA}}\n",
"example": "購(gòu)買(mǎi)地點(diǎn):TIT造艦廠(chǎng)\n購(gòu)買(mǎi)時(shí)間:2016年6月6日\(chéng)n物品名稱(chēng):咖啡\n"
}
]
}
總結(jié)
以上所述是小編給大家介紹的基于python 微信小程序之獲取已存在模板消息列表,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python環(huán)境功能強(qiáng)大的pip-audit安全漏洞掃描工具
這篇文章主要為大家介紹了python環(huán)境中功能強(qiáng)大的pip-audit安全漏洞掃描工具的功能介紹及安裝使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
Python素?cái)?shù)檢測(cè)實(shí)例分析
這篇文章主要介紹了Python素?cái)?shù)檢測(cè)方法,實(shí)例分析了Python判定素?cái)?shù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06
基于python編寫(xiě)監(jiān)控系統(tǒng)各項(xiàng)資源的腳本
這篇文章主要為大家詳細(xì)介紹了如何編寫(xiě)一個(gè)python腳本,實(shí)現(xiàn)監(jiān)控網(wǎng)絡(luò)的流量、CPU使用率、內(nèi)存使用率和磁盤(pán)使用情況,感興趣的小伙伴可以了解下2023-11-11
Python利用代理ip實(shí)現(xiàn)自動(dòng)化爬蟲(chóng)任務(wù)管理
本文主要介紹了Python利用代理ip實(shí)現(xiàn)自動(dòng)化爬蟲(chóng)任務(wù)管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Python使用keys() 獲取 Redis 數(shù)據(jù)庫(kù)中的所有鍵
哈希、列表、集合、排序集合、字符串、JSON 和流是 Redis 支持的眾多數(shù)據(jù)結(jié)構(gòu)之一,本文將討論獲取 Redis 數(shù)據(jù)庫(kù)中的所有鍵,感興趣的朋友跟隨小編一起看看吧2023-08-08

