python--微信公眾號發送消息token驗證失敗詳解


提醒大家,若不是企業公眾號,是不支持發送消息的,這一點得注意,如果單純是玩的話,建議使用測試公眾號,可以隨便玩;

地址:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

第一步:打開公眾號

 

 

 

 

第二步:搭建一個web服務器的時候要注意

 

 

這樣你就配置完了你的服務器配置了,接着點擊啟用就可以啦,弄不到企業號的話就直接使用測試公眾號玩吧;

測試賬號一般長這樣子的:

 

測試代碼也給大家貼一份:

 1 import requests
 2 import json
 3 
 4 
 5 def get_access_token():
 6     """
 7     獲取微信全局接口的憑證(默認有效期倆個小時)
 8     如果不每天請求次數過多, 通過設置緩存即可
 9     """
10     result = requests.get(
11         url="https://api.weixin.qq.com/cgi-bin/token",
12         params={
13             "grant_type": "client_credential",
14             "appid": "你的appid",
15             "secret": "你的secret",
16         }
17     ).json()
18 
19     if result.get("access_token"):
20         access_token = result.get('access_token')
21     else:
22         access_token = None
23     return access_token
24 
25 def sendmsg(openid,msg):
26     '''
27      openid(其實也就是關注你公眾號的個人微信的別名) 在你的測試公眾號賬號那是有的,或者通過接口獲取
28     獲取openid接口:"https://api.weixin.qq.com/cgi-bin/user/get?access_token={你的access_token}&next_openid="
29     '''
30     access_token = get_access_token()
31 
32     body = {
33         "touser": openid,
34         "msgtype": "text",
35         "text": {
36             "content": msg
37         }
38     }
39     '''https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=ACCESS_TOKEN'''
40     response = requests.post(
41         url="https://api.weixin.qq.com/cgi-bin/message/custom/send",
42         params={
43             'access_token': access_token
44         },
45         data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')
46     )
47 
48     result = response.json()
49     print(result)
View Code

 

看網上太多資料都不太對,於是就寫一篇文章來取悅大家了,希望大家喜歡。

還是看不懂的話可以私信我QQ:3013212379


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM