提醒大家,若不是企业公众号,是不支持发送消息的,这一点得注意,如果单纯是玩的话,建议使用测试公众号,可以随便玩;

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

第一步:打开公众号

python--微信公众号发送消息token验证失败详解_公众号

 

 

 

 

第二步:搭建一个web服务器的时候要注意

python--微信公众号发送消息token验证失败详解_企业号_02

 

 

这样你就配置完了你的服务器配置了,接着点击启用就可以啦,弄不到企业号的话就直接使用测试公众号玩吧;

测试账号一般长这样子的:

python--微信公众号发送消息token验证失败详解_公众号_03

 

测试代码也给大家贴一份:


python--微信公众号发送消息token验证失败详解_json_04python--微信公众号发送消息token验证失败详解_微信_05


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

python--微信公众号发送消息token验证失败详解_公众号_06

 

看网上太多资料都不太对,于是就写一篇文章来取悦大家了,希望大家喜欢。