概念
微信公众平台和微信开放平台的区别:
微信公众平台,是针对微信公众号。
微信开放平台,是授权登录,微信支付一类的东西。订阅号和服务号的区别(具体api的区别官方文档中也有写):
参考:https://www.cnblogs.com/lytwajue/p/7224420.html
总结一下就是一些基础的功能是相同的,但是服务号有更多的API权限
几个会用到的网址
微信公众号的管理页面:
https://mp.weixin.qq.com/cgi-bin/home测试号管理页面:
https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index官方的debug工具:
https://mp.weixin.qq.com/debug/
简单的入门
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5a
webhook 的 token验证
企鹅家叫token,一些其他的公司会把这个叫secret。就是你和微信共享的一个东西,用来验证你的服务器收到的请求确实是来自微信的。
在修改 url 和 token 并 提交 时,会收到一个echostr 的请求
/wx?signature=7a0834c290fa166757d82064353e5969de867961&echostr=411331083279914619×tamp=1527829545&nonce=2449901031
需要用 timestamp
nonce
和存储在 服务端的 token
计算signature
如果一致,则返回echostr
其实直接返回echostr
也可以提交成功
官方的例子是用python,这里有几个go的例子:
https://studygolang.com/articles/181 (比较详细)
https://blog.csdn.net/u012210379/article/details/52789525
https://tonybai.com/2014/12/18/access-validation-for-wechat-public-platform-dev-in-golang
webhook
官方收发消息的例子都是用web hook 这套流程。
简单说就是,当用户发消息给公众号,腾讯会通知你的服务器,有人想你发了消息。
这里注意的是,web hook 的body 是xml
,而微信的API的格式是json
接收消息一般没什么问题。但是发送消息可能有问题,需要把content
和 msgtype
字段加上[!cdata[]]
这个东西,并且记得加上XMLName
字段
参考:
https://studygolang.com/articles/9994
https://segmentfault.com/q/1010000008231680
type RequestData struct{
XMLName xml.Name `xml:"xml"`
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType CdataString `xml:"MsgType"`
Content CdataString `xml:"Content"`
MsgId string `xml:"MsgId"`
}
type CdataString struct {
Value string `xml:",cdata"`
}
type ReplyData struct {
XMLName xml.Name `xml:"xml"`
ToUserName string `xml:"ToUserName"`
FromUserName string `xml:"FromUserName"`
CreateTime int64 `xml:"CreateTime"`
MsgType CdataString `xml:"MsgType"`
Content CdataString `xml:"Content"`
}
web hook的ip列表1
{"ip_list":[
"101.226.62.77",
"101.226.62.78",
"101.226.62.79",
"101.226.62.80",
"101.226.62.81",
"101.226.62.82",
"101.226.62.83",
"101.226.62.84",
"101.226.62.85",
"101.226.62.86",
"101.226.103.59",
"101.226.103.60",
"101.226.103.61",
"101.226.103.62",
"101.226.103.63",
"101.226.103.69",
"101.226.103.70",
"101.226.103.71",
"101.226.103.72",
"101.226.103.73",
"140.207.54.73",
"140.207.54.74",
"140.207.54.75",
"140.207.54.76",
"140.207.54.77",
"140.207.54.78",
"140.207.54.79",
"140.207.54.80",
"182.254.11.203",
"182.254.11.202",
"182.254.11.201",
"182.254.11.200",
"182.254.11.199",
"182.254.11.198",
"59.37.97.100",
"59.37.97.101",
"59.37.97.102",
"59.37.97.103",
"59.37.97.104",
"59.37.97.105",
"59.37.97.106",
"59.37.97.107",
"59.37.97.108",
"59.37.97.109",
"59.37.97.110",
"59.37.97.111",
"59.37.97.112",
"59.37.97.113",
"59.37.97.114",
"59.37.97.115",
"59.37.97.116",
"59.37.97.117",
"59.37.97.118",
"112.90.78.158",
"112.90.78.159",
"112.90.78.160",
"112.90.78.161",
"112.90.78.162",
"112.90.78.163",
"112.90.78.164",
"112.90.78.165",
"112.90.78.166",
"112.90.78.167",
"140.207.54.19",
"140.207.54.76",
"140.207.54.77",
"140.207.54.78",
"140.207.54.79",
"140.207.54.80",
"180.163.15.149",
"180.163.15.151",
"180.163.15.152",
"180.163.15.153",
"180.163.15.154",
"180.163.15.155",
"180.163.15.156",
"180.163.15.157",
"180.163.15.158",
"180.163.15.159",
"180.163.15.160",
"180.163.15.161",
"180.163.15.162",
"180.163.15.163",
"180.163.15.164",
"180.163.15.165",
"180.163.15.166",
"180.163.15.167",
"180.163.15.168",
"180.163.15.169",
"180.163.15.170",
"101.226.103.0\/25",
"101.226.233.128\/25",
"58.247.206.128\/25",
"182.254.86.128\/25",
"103.7.30.21",
"103.7.30.64\/26",
"58.251.80.32\/27",
"183.3.234.32\/27",
"121.51.130.64\/27",
"101.91.60.96\/28",
"223.166.222.11",
"223.166.222.104\/29",
"183.192.172.11",
"183.192.172.96\/28",
"58.60.9.12",
"58.60.9.112\/28",
"157.255.192.12",
"157.255.192.112\/28",
"121.51.162.112\/28"
]}
API
普通的json请求没啥好说的。
先用appid 和 app secret 获得动态access token,拿到后就可以用此token访问api 接口了。
注意这里的acess token 和前面的token是两个东西,这个token是你向微信证明你有读取数据的权限。
access_token 的那个IP白名单可以不用设置好像。
access_token 刷新时间是7200s好像。
access_token 和 客服消息接口 的例子:
https://studygolang.com/articles/2214