golang微信公众平台之消息接入

API文档上的内容

注册公众平台什么的就不说了,消息接入部分,文档地址:

http://mp.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97#.E7.94.B3.E8.AF.B7.E6.B6.88.E6.81.AF.E6.8E.A5.E5.8F.A3

简言之就是,提交申请,微信服务器将发送GET请求到你所填写的URL上,这个GET请求附带四个参数:

参数 描述
signature 微信加密签名
timestamp 时间戳
nonce 随机数
echostr 随机字符串

过程很简单,校验数据,确认请求来自微信服务器,则原样返回echostr参数内容,接入生效。

校验流程:

1. 将token、timestamp、nonce三个参数进行字典序排序
2. 将三个参数字符串拼接成一个字符串进行sha1加密
3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

GO搭建一个web服务器

https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/03.2.md上的内容,代码直接拿过来用了:

package main

import (
    "fmt"
    "net/http"
    "log"
)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello!") 
}

func main() {
    http.HandleFunc("/", sayhelloName) //设置访问的路由
    err := http.ListenAndServe(":80", nil) //设置监听的端口
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

当然我这里删掉了不少东西,只留下最简单的框架,端口也改成了80,因为文档里要求80端口。

这样一个简单的服务器,跑起来,浏览器中输入http://localhost,会输出Hello!

这个框架保持不变,我们所需要修改的部分就是sayhelloName,不过根据用途改个名字好些,checkSignature吧。

实现checkSignature

接下来的事就水到渠成了,跟着校验流程实现checkSignature

第一步:必然,先把GET请求附带的数据取出来

r.ParseForm()
var token string="你的token"
var signature string=strings.Join(r.Form["signature"],"")
var timestamp string=strings.Join(r.Form["timestamp"],"")
var nonce string=strings.Join(r.Form["nonce"],"")
var echostr string=strings.Join(r.Form["echostr"],"")

第二步:字典排序

tmps:=[]string{token,timestamp,nonce}
sort.Strings(tmps)

第三步:sha1加密

func str2sha1(data string)string{
    t:sha1.New()
    io.WriteString(t,data)
    return fmt.Sprintf("%x",t.Sum(nil))
}

第四步:对比signature

完整的代码如下:

func checkSignature(w http.ResponseWriter,r *http.Request){
    r.ParseForm()
    var token string="你的token"
    var signature string=strings.Join(r.Form["signature"],"")
    var timestamp string=strings.Join(r.Form["timestamp"],"")
    var nonce string=strings.Join(r.Form["nonce"],"")
    var echostr string=strings.Join(r.Form["echostr"],"")
    tmps:=[]string{token,timestamp,nonce}
    sort.Strings(tmps)
    tmpStr:=tmps[0]+tmps[1]+tmps[2]
    tmp:=str2sha1(tmpStr)
    if tmp==signature{
        fmt.Fprintf(w,echostr)
    }
}
posted @ 2013-06-01 17:57  trirocky  阅读(2780)  评论(0编辑  收藏  举报